解决python requests包的post提交形式form和json不同导致传参 is null

requests包在Python中用于发送HTTP请求,POST方法支持form表单和JSON数据提交。区别在于Content-Type头,form使用application/x-www-form-urlencoded,而JSON则需设置为application/json。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

requests包是python语言里的一个发送请求的很好用的包,它集成了很多http请求方式,本文重点介绍一下post的提交形式。

服务端的API有的接受form表单提交,有的接受json数据提交,他们的python编写区别如下:

form:

header = {
    "Content-Type": "application/x-www-form-urlencoded"
}

requests.post(url , data={"appname": '111'}, headers=header)

json:

header = {
    "Content-Type": "application/json"
}

requests.post(url , data={"appname": '111'}, headers=header)

两者的主要区别在于headers里。

Python中,可以使用`requests.post`方法来发送POST请求。有几种不同的用法可以根据你的需求选择。 第一种方式是使用`x-www-form-urlencoded`格式的数据发送POST请求。你可以使用一个字典来表示要发送的数据,然后将该字典作为`data`参数传递给`requests.post`方法。例如: ```python import requests url = "https://editor.net/" data = {"key": "value"} # 字典格式,外层无引号 resp = requests.post(url, data=data) print(resp.text) ``` 第二种方式是使用Python模拟浏览器发送POST请求。你可以使用`requests.post`方法,并通过`data`参数传递要发送的数据。与第一种方式不同的是,该参数可以接受字符串作为输入。例如: ```python import requests url = "https://editor.net/" data = '{"key": "value"}' # 字符串格式 resp = requests.post(url, data=data) print(resp.text) ``` 除了上述两种方式,还可以使用`json`参数来发送POST请求。你可以将要发送的数据转换为JSON格式的字符串,并将其作为`data`参数传递给`requests.post`方法。例如: ```python import requests url = "https://editor.net/" data = {"key": "value"} # 字典格式 resp = requests.post(url, json=data) print(resp.text) ``` 以上就是Python中`requests.post`方法的用法。你可以根据不同的需求选择适合的方式来发送POST请求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python requests post 使用方法](https://blog.youkuaiyun.com/qq_42777433/article/details/105829795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值