当通过form表单发送POST请求时,默认的Content-Type是application/x-www-form-urlencoded
,而不是application/json
。因此,如果你想在发送POST请求时使用application/json
作为Content-Type,你需要手动设置请求头。
在代码中添加以下行来设置请求头:
headers = {'Content-Type': 'application/json'}
然后,在发送POST请求时,你需要将这个headers
参数传递给requests库的post
方法,示例代码如下:
import requests
url = 'https://example.com/your-endpoint'
data = {
'key1': 'value1',
'key2': 'value2'
}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=data, headers=headers)
print(response.text)
这样,你就可以在发送POST请求时使用application/json
作为Content-Type,并将请求数据以JSON格式发送。
请注意,上述示例代码中,我们使用了requests库,你可能需要根据自己的开发环境和需求进行相应的安装和引入。此外,你需要根据实际情况替换url
和data
的值。