如何在点击 post api 时更改 python 中的内容类型?

问题描述 投票:0回答:1

我正在使用第三方API,我需要通过在发布请求中发送txt文件来创建批处理。

params= {'key':'value'}
url = 'htpps://gecoding.com'   //it just a exammple
headers = {'Api-key'='xyz','content-type':'multipart/form-data'}
files = {'file' : open (r'file_path','rb')}
r=request.post(url=url, files=files, params=params, headers=headers, verify=False)

发出请求后,我尝试查看标头,它显示 application/json

print(r.headers['content-type']
由于这个我的反应影响。我尝试了很多东西但无法改变内容类型 谁能帮忙设置一下

我希望“内容类型”为“多部分/表单数据”,但获取应用程序/json

python python-requests
1个回答
0
投票

如果你想发送json数据,你应该通过

json
参数设置body,而不是
files
参数:

r=request.post(url=url, json=json, params=params, headers=headers, verify=False)

这将导致请求库自动为您设置适当的内容类型标头(就像当前通过基于

multipart/form-data
参数设置
files
所做的那样)

© www.soinside.com 2019 - 2024. All rights reserved.