python requests post json_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,json = 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值