今天在用requests模塊作api接口的測試,發現傳送簡單字段,以下所示,後臺均能接收成功。json
import requests data = {"version":"1.0.1","collections":[{"type":4,"newsId":1},{"type":1,"newsId":2}]} requests.post(url='http://xx.com', data=data).text
post請求後,後臺只能接收version的參數,而collections的參數接收不正確。api
解決方法,加入header頭,指定傳送參數爲json類型,同時將data由字典轉爲json字符串傳送app
import requests import json headers = {'Content-Type': 'application/json'} data = {"version":"1.0.1","collections":[{"type":4,"newsId":1},{"type":1,"newsId":2}]} requests.post(url='http://xx.com', data=json.dumps(data),headers=headers).text