开发测试数据-通过API批量生成
背景
在日常开发过程中,可能需要一些批量的用户数据,方便开发与测试。
Random User是一个基于Web 的api服务
从网址上就能理解,它是用来帮我们生成用户信息的。
简单使用
调用api
https://randomuser.me/api/
在浏览器上输入url,并打开
获取多个用户数据
默认是一个,要想获得多个用户数据,需要加上参数
result=[num]
例如:
https://randomuser.me/api/?results=5
指定某个国家的数据
有时候会发现有些姓名是其他国家的语言,为了方便可以添加参数来选择想要的国家语言
- v1.0: AU, BR, CA, CH, DE, DK, ES, FI, FR, GB, IE, IR, NL, NZ, TR, US
- v1.1: AU, BR, CA, CH, DE, DK, ES, FI, FR, GB, IE, IR, NL, NZ, TR, US
- v1.2: AU, BR, CA, CH, DE, DK, ES, FI, FR, GB, IE, IR, NO, NL, NZ, TR, US
- v1.3: AU, BR, CA, CH, DE, DK, ES, FI, FR, GB, IE, IR, NO, NL, NZ, TR, US
nat=[country]
例如:
https://randomuser.me/api/?results=5&nat=us
或者多个国家
https://randomuser.me/api/?results=5&nat=us,dk,fr,gb
指定性别的数据
比如我只需要性别为女的数据,添加参数
gender=[female]
https://randomuser.me/api/?results=5&nat=ca&gender=female
自定义密码数据
定义大小写字母以及最长16位
password=upper,lower,1-16
https://randomuser.me/api/?results=5&password=upper,lower,1-16
自定义格式指定密码选项
special !"#$%&'()*+,- ./:;<=>?@[\]^_`{|}~
upper ABCDEFGHIJKLMNOPQRSTUVWXYZ
lower abcdefghijklmnopqrstuvwxyz
number 0123456789
下面是一些密码选项组合的更多示例:
// Special chars exactly 32 characters long
https://randomuser.me/api/?password=special,32
// Uppercase chars between 1 to 8 characters long
https://randomuser.me/api/?password=upper,1-8
// Special, uppercase, lowercase, and numeric chars between the default 8 to 64 characters long
https://randomuser.me/api/?password=special,upper,lower,number
使用format参数指定要返回的格式
目前提供以下数据格式:
- JSON (default)
- PrettyJSON or pretty
- CSV
- YAML
- XML
例如:
返回csv格式
https://randomuser.me/api/?results=5&format=csv
自动下载csv文件
可使用的参数
- gender
- name
- location
- login
- registered
- dob
- phone
- cell
- id
- picture
- nat
使用inc和exc参数,可以分别指定要包括或排除的字段
指定包含的字段inc
inc=[field1],[[field2],[field3]...
例如:
包含 性别、姓名、国家的字段
https://randomuser.me/api/?results=5&inc=gender,name,nat
指定去除某某字段exc
exc=[field1],[[field2],[field3]...
例如:
去除 性别、姓名、国家的字段
https://randomuser.me/api/?results=5&exc=gender,name,nat
{
"results": [{
"location": {
"street": {
"number": 456,
"name": "Godthåbsvej"
},
"city": "Nørre Sundby",
"state": "Danmark",
"country": "Denmark",
"postcode": 68776,
"coordinates": {
"latitude": "-33.5221",
"longitude": "-0.5537"
},
"timezone": {
"offset": "-8:00",
"description": "Pacific Time (US & Canada)"
}
},
"email": "felix.rasmussen@example.com",
"login": {
"uuid": "4e20390a-37b5-4843-bd68-a2f3f2ae16e6",
"username": "greenleopard674",
"password": "sunny",
"salt": "lDRwFsmm",
"md5": "5ed23d9b6fbc6c3366bc5bd16ef7f5e9",
"sha1": "c601894362db9ebb42782f87216eb3ae877d3633",
"sha256": "69853305f1fa0707400ae3ad5e9177c9e589310ff65d3d31b1c3f8864d071dd2"
},
"dob": {
"date": "1952-02-09T06:00:18.559Z",
"age": 69
},
"registered": {
"date": "2019-03-21T22:31:06.919Z",
"age": 2
},
"phone": "91680394",
"cell": "34790668",
"id": {
"name": "CPR",
"value": "090252-5830"
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/22.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/22.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/22.jpg"
}
}, {
"location": {
"street": {
"number": 1936,
"name": "North Street"
},
"city": "Midleton",
"state": "Kildare",
"country": "Ireland",
"postcode": 84070,
"coordinates": {
"latitude": "-28.3490",
"longitude": "-8.0961"
},
"timezone": {
"offset": "+11:00",
"description": "Magadan, Solomon Islands, New Caledonia"
}
},
"email": "maurice.sims@example.com",
"login": {
"uuid": "bf19ad8d-61a7-4bdf-b8a6-d17b3889a26d",
"username": "happysnake924",
"password": "bigblock",
"salt": "eDTlGFMO",
"md5": "99a3cc04a0ac85d24383c260749bbf8e",
"sha1": "9239d98df10712c9fa1503e7a5230948e3c3eff5",
"sha256": "3d1b355afb15a8412b2db281ab787b4f1a8e56cfaf9b90d484f4f0c3ec2a91bf"
},
"dob": {
"date": "1955-10-30T20:11:34.496Z",
"age": 66
},
"registered": {
"date": "2007-08-15T14:14:07.202Z",
"age": 14
},
"phone": "041-322-2439",
"cell": "081-581-8150",
"id": {
"name": "PPS",
"value": "1774264T"
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/94.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/94.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/94.jpg"
}
}, {
"location": {
"street": {
"number": 5344,
"name": "Markedsgade"
},
"city": "Aarhus N",
"state": "Hovedstaden",
"country": "Denmark",
"postcode": 98213,
"coordinates": {
"latitude": "74.8077",
"longitude": "-105.6085"
},
"timezone": {
"offset": "-10:00",
"description": "Hawaii"
}
},
"email": "asta.pedersen@example.com",
"login": {
"uuid": "4f844347-4e9d-42d1-912c-c64a7491b818",
"username": "orangedog626",
"password": "notnow",
"salt": "T80lAC3S",
"md5": "a38730ae5f45cca4729a097e2a20fedf",
"sha1": "a9800224882f2145c22c5e748cc311900b4b06c2",
"sha256": "0ecd99b41a708b238243f097d029cbe40fc97882bed54645d753a4346b50ec74"
},
"dob": {
"date": "1960-09-05T09:59:51.450Z",
"age": 61
},
"registered": {
"date": "2016-08-26T14:38:57.116Z",
"age": 5
},
"phone": "45254393",
"cell": "98966894",
"id": {
"name": "CPR",
"value": "050960-5010"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/91.jpg",
"medium": "https://randomuser.me/api/portraits/med/women/91.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/women/91.jpg"
}
}, {
"location": {
"street": {
"number": 2454,
"name": "Marktplatz"
},
"city": "Schwarzenbach am Wald",
"state": "Rheinland-Pfalz",
"country": "Germany",
"postcode": 86235,
"coordinates": {
"latitude": "2.8458",
"longitude": "-167.6357"
},
"timezone": {
"offset": "0:00",
"description": "Western Europe Time, London, Lisbon, Casablanca"
}
},
"email": "siegfried.spangenberg@example.com",
"login": {
"uuid": "4fbff367-eba8-4294-9f79-4876e6edfbad",
"username": "heavyfish597",
"password": "werdna",
"salt": "ceJ0qAC5",
"md5": "745deba77342d1bcfad6d06b7e0a6f8d",
"sha1": "5485b39271950c0890a9a5afe36e65eeb5057d03",
"sha256": "5f6f2ae3a39c2227eee2b159b2b92e877a7e53467b1b58474e588735add4841d"
},
"dob": {
"date": "1959-03-15T05:02:19.312Z",
"age": 62
},
"registered": {
"date": "2009-09-07T05:09:30.290Z",
"age": 12
},
"phone": "0339-3514855",
"cell": "0176-1304246",
"id": {
"name": "",
"value": null
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/4.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/4.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/4.jpg"
}
}, {
"location": {
"street": {
"number": 616,
"name": "Rue Saint-Georges"
},
"city": "Mauensee",
"state": "Thurgau",
"country": "Switzerland",
"postcode": 9009,
"coordinates": {
"latitude": "79.8831",
"longitude": "-12.3923"
},
"timezone": {
"offset": "+3:30",
"description": "Tehran"
}
},
"email": "juliette.deschamps@example.com",
"login": {
"uuid": "912147dd-739c-4ad0-99f4-67ef74846055",
"username": "happymouse460",
"password": "skydive",
"salt": "B3v0mftj",
"md5": "038cddf2e8b7dc04817ec744c25b4433",
"sha1": "797dbebce443255e1195850a0c2c6a0be671d8a4",
"sha256": "73ff890822be1a23333d152258fc52b15e73c977c520da307dfb6af9bb3b4738"
},
"dob": {
"date": "1972-01-28T22:03:55.242Z",
"age": 49
},
"registered": {
"date": "2004-06-07T09:40:16.501Z",
"age": 17
},
"phone": "075 978 86 41",
"cell": "078 712 87 06",
"id": {
"name": "AVS",
"value": "756.1527.7228.03"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/32.jpg",
"medium": "https://randomuser.me/api/portraits/med/women/32.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/women/32.jpg"
}
}],
"info": {
"seed": "ebd7f54854602f5e",
"results": 5,
"page": 1,
"version": "1.3"
}
}