Python 中request以json形式发送post请求

一个http请求包括三个部分,为别为请求行,请求报头,消息主体,类似以下这样:

  • 请求行
  • 请求报头
  • 消息主体

以json串提交数据,编码格式:application/json, 可以将一json串传给requests.post()的data参数

=== 案例一 ====

import requests

import json

headers = {

"Content-Type": "application/json; charset=UTF-8",

"Referer": "http://jinbao.pinduoduo.com/index?page=5",

"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",

}

url = "http://jinbao.pinduoduo.com/network/api/common/goodsList"

pyload = {"keyword": "", "sortType": 0, "withCoupon": 0, "categoryId": 16, "pageNumber": 1, "pageSize": 60}

response = requests.post(url, data=json.dumps(pyload), headers=headers).text

print(response)

=== 案例二 ====

import requests
import json
MSISDN = input('请输入msisdn: ')
headers = {
"Content-Type": "application/json",
"Postman-Token": "255b9cb2-36a4-46b1-a341-2a147c94788a",
"cache-control": "no-cache",
}
url = "http://operator-xxxxxx"
payload = {"msisdn":MSISDN}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)
特别感谢:https://blog.csdn.net/weixin_43131464/article/details/82766690