python 请求了一个接口,请求头的按照格式打包好了,请求还是400
import requests"""
https://www.cnblogs.com/#p200
"""
post_json = {
"CategoryId": 808,
"CategoryType": "SiteHome",
"ItemListActionName": "AggSitePostList",
"PageIndex": 10,
"ParentCategoryId": 0,
"TotalPostCount": 4000
}
headers = {
#":authority": "www.cnblogs.com"
#,":method": "POST"
#":path": "/AggSite/AggSitePostList"
#,":scheme": "https"
"accept": "text/plain, */*; q=0.01"
,"accept-encoding": "gzip, deflate, br"
,"accept-language": "th,zh;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7"
,"content-length": "140"
,"content-type": "application/json; charset=UTF-8"
,"cookie": ""
,"origin": "https://www.cnblogs.com"
,"referer": "https://www.cnblogs.com/"
,"sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"'
,"sec-ch-ua-mobile": "?0"
,"sec-ch-ua-platform": "Windows"
,"sec-fetch-dest": "empty"
,"sec-fetch-mode": "cors"
,"sec-fetch-site": "same-origin"
,"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"
,"x-requested-with": "XMLHttpRequest"
}
res2 = requests.post('https://www.cnblogs.com/AggSite/AggSitePostList', data= post_json, headers=headers)
print(res2)
https://www.cnblogs.com/#p200 原页面, 有个异步加载接口
分析得到了一个请求接口 https://www.cnblogs.com/AggSi...
格式照抄了,请求400怎么解决,
求教! 还差了什么参数?
已经解决了, 网上抄了一个post 配置导致的,
data = post_json, 这里的data= 改成 json= 来提交参数就可以了
回答:
提交 post 表单,就是 content_type 是 application/x-www-form-urlencoded
或者 multipart/form-data
就要用 requests 的 data 参数
纯表单就是application/x-www-form-urlencoded
, 要提交表单的同时带上文件,就是multipart/form-data
提交 post 是 json ,即 content_type 是 application/json
就要用 requests 的 json 参数
怎么判断呢?
看请求头中的 Content-Type
以上是 python 请求了一个接口,请求头的按照格式打包好了,请求还是400 的全部内容, 来源链接: utcz.com/p/938498.html