requests.post方法中data与json参数区别

 

  Requests参数

1. 先能够看一下requests的源码:html

1
2
3
4
5
6
7
8
9
10
11
12
13
def post(url, data=None, json=None, **kwargs):
     r "" "Sends a POST request.
 
     :param url: URL  for  the  new  : class :`Request`  object .
     :param data: (optional) Dictionary, list of tuples, bytes, or file-like
         object  to send  in  the body of the : class :`Request`.
     :param json: (optional) json data to send  in  the body of the : class :`Request`.
     :param \*\*kwargs: Optional arguments that ``request`` takes.
     : return : : class :`Response <Response>`  object
     :rtype: requests.Response
     "" "
 
     return  request( 'post' , url, data=data, json=json, **kwargs)

 

说明:python

  从源码中注释看,告诉咱们post请求报文中既能够传data,也能够传json。而且data与json,既能够是str类型,也能够是dict类型。web

 

2. json与data参数规则:django

1、JSONjson

1.使用json参数,无论报文是str类型,仍是dict类型,若是不指定headers中content-type的类型,默认是:application/json。服务器

2、DATAapp

1.使用data参数,报文是dict类型,若是不指定headers中content-type的类型,默认application/x-www-form-urlencoded,至关于普通form表单提交的形式,会将表单内的数据转换成键值对,此时数据能够从request.POST里面获取,而request.body的内容则为a=1&b=2的这种键值对形式。post

注意:即便指定content-type=application/json,request.body的值也是相似于a=1&b=2,因此并不能用json.loads(request.body.decode())获得想要的值。url

2. 使用data参数,报文是str类型,若是不指定headers中content-type的类型,默认application/json。
 

综上所述,两种参数的使用状况:spa

用data参数提交数据时,request.body的内容则为a=1&b=2的这种形式,用json参数提交数据时,request.body的内容则为'{"a": 1, "b": 2}'的这种形式

 

  实例解析

  知道了上面所讲的理论,须要经过实例来验证一下。

以我django项目开发的web接口为例:

url.py文件

1
2
3
4
5
6
7
8
9
10
from  django.contrib import admin
from  django.urls import path,include
from  django_web.views import views
from  django.conf.urls import url
 
urlpatterns = [
     path( 'admin/' , admin.site.urls),
 
     #首页
     path( 'index/' ,views.test_dj), #登录页面

view.py文件

1
2
3
4
5
6
7
8
9
def test_dj(request):
     print(request.body,111)
     print(request.headers)
  
"" "
     当post请求的请求体以data为参数,发送过来的数据格式为:b 'username=test&password=123'
     当post请求的请求体以json为参数,发送过来的数据格式为:b '{"username": "test", "password": "123"}'
"" "
return  render(request,  'index.html' )

  

exp1:

test.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests
 
headers = { 'content-type' : 'application/json' }
 
data = {
     "username" "test" ,
 
     "password" "123"
}
 
print(type(data)) #dict
r1 = requests.post(url= "http://127.0.0.1:8000/index/" ,data=data)
 
 
print(r1.text)

 

返回的报文:

1
2
b 'username=test&password=123'  111
{ 'Content-Length' '26' 'Content-Type' 'application/x-www-form-urlencoded' 'Host' '127.0.0.1:8000' 'User-Agent' 'python-requests/2.22.0' 'Accept-Encoding' 'gzip, deflate' 'Accept' '*/*' 'Connection' 'keep-alive' }

 

exp1案例:证明了第二条规则:使用data参数,报文是dict类型,不指定content-type,默认是:application/x-www-form-urlencoded,请求数据格式是:key1=value1&key2=value2键值对形式。

 

exp2:

test.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests
 
headers = { 'content-type' : 'application/json' }
 
data = {
     "username" "test" ,
 
     "password" "123"
}
 
print(type(data)) #dict
r1 = requests.post(url= "http://127.0.0.1:8000/index/" ,json=data)
 
 
print(r1.text)

 

将参数data换成json请求,返回的报文:

1
2
b '{"username": "test", "password": "123"}'  111
{ 'Content-Length' '39' 'Content-Type' 'application/json' 'Host' '127.0.0.1:8000' 'User-Agent' 'python-requests/2.22.0' 'Accept-Encoding' 'gzip, deflate' 'Accept' '*/*' 'Connection' 'keep-alive' }

exp2案例:证明了第一条规则:使用json参数,报文是dict类型,若是不指定headers中content-type的类型,默认是:application/json,请求数据格式是:dict形式。

 

exp3:

test.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import requests,json
 
headers = { 'content-type' : 'application/json' }
 
data = {
     "username" "test" ,
 
     "password" "123"
}
 
print(type(data)) #dict
 
# 方法1
r1 = requests.post(url= "http://127.0.0.1:8000/index/" ,data=json.dumps(data))
# 方法2
r2 = requests.post(url= "http://127.0.0.1:8000/index/" ,json=json.dumps(data))
 
 
print(r1.text)
print(r2.request)

 

查看返回报文:

1
2
3
4
5
b '{"username": "test", "password": "123"}'  111
{ 'Content-Length' '39' 'Content-Type' 'text/plain' 'Host' '127.0.0.1:8000' 'User-Agent' 'python-requests/2.22.0' 'Accept-Encoding' 'gzip, deflate' 'Accept' '*/*' 'Connection' 'keep-alive' }
b '"{\\"username\\": \\"test\\", \\"password\\": \\"123\\"}"'  111
{ 'Content-Length' '49' 'Content-Type' 'application/json' 'Host' '127.0.0.1:8000' 'User-Agent' 'python-requests/2.22.0' 'Accept-Encoding' 'gzip, deflate' 'Accept' '*/*' 'Connection' 'keep-alive' }
[01/Mar/2020 14:02:11]  "POST /index/ HTTP/1.1"  200 1890

  

exp3案例

证明了第一条规则:使用json参数,报文是str类型,若是不指定headers中content-type的类型,默认是:application/json,请求数据格式是:dict形式。也就是test.py中的方法2.

还证明了第三条规则:使用data参数,报文是str类型,若是不指定headers中content-type的类型,默认application/json。其实方法1和方法2是等价的。报文是json字符串数据,分别以data与json两种参数形式发送请求,获得的请求体数据格式是同样。

 

注意:方法1返回的数据类型是:'Content-Type': 'text/plain',表示无格式,但规则说是默认application/json,是否有冲突? 其实否则,服务器并无强制指定接收数据的格式类型,因此这种状况下也不会报错。

 

 

 

  总结

  总而言之,记住这句话:用data参数提交数据时,request.body的内容则为a=1&b=2的这种形式,用json参数提交数据时,request.body的内容则为'{"a": 1, "b": 2}'的这种形式。

相关文章
相关标签/搜索