我有一个MVC Web Api,我可以用以下请求添加新记录"简单的REST客户端"
URL: http://localhost:4585/api/users
Headers: Content-Type: application/json; charset=utf-8
Data:
[{
"username": "name1",
"email": "mail1",
"password": "password1"
},
{
"username": "name2",
"email": "mail2",
"password": "password2"
}]
我想要的是用python做同样的事情。我尝试过以下代码,但API只处理了第一条记录。
import json
import urllib2
data = [{ "username": "name1", "email": "mail1", "password": "password1" }, { "username": "name2", "email": "mail2", "password": "password2" }]
response = urllib2.urlopen(req, json.dumps(data))
答案 0 :(得分:0)
我用请求库做了。
import requests
requests_session = requests.session()
requests_session.headers.update({'Content-Type': 'application/json'})
requests_session.headers.update({'charset':'utf-8'})
post_data = '[{ "username": "name1", "email": "mail1", "password": "password1" }, { "username": "name2", "email": "mail2", "password": "password2" }]'
requests_response = requests_session.post(url="http://localhost:4585/api/users", data=post_data)
print requests_response.content