python3 post json_python3 的 request.post () 方法中,传参 data 与 json 的区别

python3 的 request.post() 方法的源码:

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 (will be form-encoded), 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 ` object

:rtype: requests.Response

"""

return request('post', url, data=data, json=json, **kwargs)

data 和 json 参数既可以是 str,也可以是 dict。它们的区别如下:

json 不管是 str 还是 dict,如果不指定 headers 中的 content-type,默认为 application/json

data 为 dict 时,如果不指定 headers 中的 content-type,默认为 application/x-www-form-urlencoded,相当于普通 form 表单提交的形式,此时数据可以从 request.post 里面获取,而 request.body 的内容则为 a=1&b=2 的这种形式.注意,即使指定 content-type=application/json,request.body 的值也是类似于 a=1&b=2,所以并不能用 json.loads(request.body.decode()) 得到想要的值

data 参数为 str 时,如果不指定 headers 中的 content-type,默认为 application/json

以下测试例子:

1、以【空气质量发布】iOS 端 app 为例,先抓包,启动 app,首页 url 为:http://epapi.moji.com/json/home/homePage, 如图可以看到 request 对应的 headers 和 data 情况:

c312a123d668a3d3635cf184f1a2297e.png

2、在代码中添加 headers 和 data,完整测试代码如下,它有两种实现方式:

# -*- coding: utf-8 -*-

# @file: test_requests.py.py

# @author: xiaoxiao

# @date : 2019/12/19

import requests

import json

# 统一的headers

my_headers = {

'Accept': '*/*',

'User-Agent': 'AirMonitoring/3.0.6 (iPhone; iOS 11.4.1; Scale/2.00)',

'Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, zh-Hant-HK;q=0.7',

'Content-Type': 'application/json',

'Content-Length': '383',

'Host': 'epapi.moji.com',

'Accept-Encoding': 'gzip',

'Connection': 'keep-alive'

}

# 统一的data

my_data = {

'common': {

'cityid': '110000',

'app_version': '45030006',

'device': 'iPhone9,1',

'apnsisopen': '0',

'platform': 'iPhone',

'uid': 2154359387456724365,

'language': 'CN',

'identifier': '95DD96CD-A3BD-48C4-A507-E63FBBDED29C',

'token': '<545a468e 92bed312 6fe0add2 999d3a52 b9d4422a 867060c2 2d676371 b0ec5ef9>'

},

'params': {

'longitude': '116.2991775173611',

'cityId': '110000',

'latitude': '40.05391682942708'

}

}

# 空气质量发布app的首页请求

top_request = 'http://epapi.moji.com/json/home/homePage'

# @allure.feature('空气质量发布app的首页')

def test_top():

# 方法1:采用json:dict

print(type(my_data))

s = requests.post(top_request, json=my_data)

print(s)

# 方法二:通过json.dumps把json:dict转换为data:str

print(type(json.dumps(my_data)))

s = requests.post(top_request, headers=my_headers, data=json.dumps(my_data))

print(s)

s = json.loads(s.text)

print('Response:' + str(s))

assert s['code'] == 0 # 校验接口返回是否正常

if __name__ == '__main__':

test_top()

# pytest.main()

最终结果输出:

Response:{'current': {'aqi': 38, 'pm25': 13.0, 'pm10': 38.0, 'o3': 12.0, 'so2': 2.0, 'no2': 48.0, 'co': 0.6, 'nearestStationAqi': 58, 'time': '12/20 09:00', 'aqiLevel': 1, 'maxPollution': '', 'tips': '各类人群可正常活动。', 'tipsLevel': 1, 'condition': '晴', 'temperature': '-4', 'windPowder': '2级', 'humidity': '35', 'today': {'condition': '晴', 'minAqi': 60, 'maxAqi': 80, 'maxPollution': 'NO₂', 'conditionIco': 0, 'tips': '极少数异常敏感人群应减少户外活动。', 'tipsLevel': 2}, 'tomorrow': {'condition': '多云', 'minAqi': 90, 'maxAqi': 110, 'maxPollution': 'PM2.5', 'conditionIco': 1, 'tips': '极少数异常敏感人群应减少户外活动。', 'tipsLevel': 2}}, 'trendList24aqi': {'list': [{'time': '11:00', 'value': '23', 'id': 0}, {'time': '12:00', 'value': '17', 'id': 1}, {'time': '13:00', 'value': '17', 'id': 2}, {'time': '14:00', 'value': '18', 'id': 3}, {'time': '15:00', 'value': '17', 'id': 4}, {'time': '16:00', 'value': '17', 'id': 5}, {'time': '17:00', 'value': '22', 'id': 6}, {'time': '18:00', 'value': '21', 'id': 7}, {'time': '19:00', 'value': '23', 'id': 8}, {'time': '20:00', 'value': '20', 'id': 9}, {'time': '21:00', 'value': '18', 'id': 10}, {'time': '22:00', 'value': '18', 'id': 11}, {'time': '23:00', 'value': '14', 'id': 12}, {'time': '00:00', 'value': '13', 'id': 13}, {'time': '01:00', 'value': '13', 'id': 14}, {'time': '02:00', 'value': '16', 'id': 15}, {'time': '03:00', 'value': '21', 'id': 16}, {'time': '04:00', 'value': '21', 'id': 17}, {'time': '05:00', 'value': '22', 'id': 18}, {'time': '06:00', 'value': '24', 'id': 19}, {'time': '07:00', 'value': '26', 'id': 20}, {'time': '08:00', 'value': '32', 'id': 21}, {'time': '09:00', 'value': '38', 'id': 22}]}, 'forecastWeatherData7': [{'dayTitle': '昨天', 'day': '12/19', 'minAqi': 40, 'maxAqi': 60, 'maxPollution': 'NO₂', 'condition': '晴', 'conditionIco': 0}, {'dayTitle': '今天', 'day': '12/20', 'minAqi': 60, 'maxAqi': 80, 'maxPollution': 'NO₂', 'condition': '晴', 'conditionIco': 0}, {'dayTitle': '明天', 'day': '12/21', 'minAqi': 90, 'maxAqi': 110, 'maxPollution': 'PM2.5', 'condition': '多云', 'conditionIco': 1}, {'dayTitle': '星期日', 'day': '12/22', 'minAqi': 80, 'maxAqi': 95, 'maxPollution': 'PM2.5', 'condition': '晴', 'conditionIco': 0}, {'dayTitle': '星期一', 'day': '12/23', 'minAqi': 70, 'maxAqi': 90, 'maxPollution': 'PM2.5', 'condition': '多云', 'conditionIco': 1}, {'dayTitle': '星期二', 'day': '12/24', 'minAqi': 130, 'maxAqi': 150, 'maxPollution': 'PM2.5', 'condition': '阴', 'conditionIco': 2}, {'dayTitle': '星期三', 'day': '12/25', 'minAqi': 80, 'maxAqi': 100, 'maxPollution': 'PM2.5', 'condition': '晴', 'conditionIco': 0}], 'updateDate': '1576808550094', 'cityCenterLongitude': 116.407112, 'cityCenterLatitude': 39.904138, 'code': 0, 'rc': {'c': 0}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值