python requests.post中data和json的区别
使用python的requests库作一个登陆的post请求,代码如下:
import requests
import json
def login():
url = ‘http://192.168.22.1:8090/login’
pdata = {"userName": "lidaxia","password":"loginpsd"}
res = requests.post(url,data = json.dumps(pdata))
在postman中,body使用json编码,可以发送成功,但是在python中一直因为请求体格式不对报错
后经过百度,将代码改成以下:
import requests
def login():
url = ‘http://192.168.22.1:8090/login’
pdata = {"userName": "lidaxia","password":"loginpsd"}
res = requests.post(url,json = pdata)
问题解决!
认真了解了requests.post的几个参数,官方文档如下:
…………………………………………分割线…………………………………………………
…………………………………………分割线…………………………………………………
More complicated POST requests
Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made:
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("https://httpbin.org/post", data=payload)
>>> print(r.text)
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
The data argument can also have multiple values for each key. This can be done by making data either a list of tuples or a dictionary with lists as values. This is particularly useful when the form has multiple elements that use the same key:
>>> payload_tuples = [('key1', 'value1'), ('key1', 'value2')]
>>> r1 = requests.post('https://httpbin.org/post', data=payload_tuples)
>>> payload_dict = {'key1': ['value1', 'value2']}
>>> r2 = requests.post('https://httpbin.org/post', data=payload_dict)
>>> print(r1.text)
{
...
"form": {
"key1": [
"value1",
"value2"
]
},
...
}
>>> r1.text == r2.text
True
There are times that you may want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.
For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data:
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> r = requests.post(url, data=json.dumps(payload))
Instead of encoding the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically:
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> r = requests.post(url, json=payload)
Note, the json parameter is ignored if either data or files is passed.
Using the json parameter in the request will change the Content-Type in the header to application/json.
…………………………………………分割线…………………………………………………
…………………………………………分割线…………………………………………………
看完官方文档, res = requests.post(url,data = dumps(pdata))和res = requests.post(url,json = pdata)的区别为,使用参数是json时会将请求中的Content-Type改成application/json,于是修改代码去验证:
import requests
import json
def login():
url = ‘http://192.168.22.1:8090/login’
pdata = {"userName": "lidaxia","password":"loginpsd"}
headers = {"Content-Type":"application/json"}
res = requests.post(url,data = json.dumps(pdata),headers = headers)
也没有问题,所以,综上所述,如果post请求体是json格式的,使用json参数会更简单,推荐使用!
更多推荐
所有评论(0)