Python調(diào)用http-post接口的實現(xiàn)方式
Python調(diào)用http-post接口

- 引入requests包
- 使用post方法,將post請求發(fā)出
- response變量接收接口的返回值
Python通過post調(diào)用接口報400
遇到個坑,簡單記錄一下!
res = {'a':1, 'b':2}
headers = {'content-type': 'application/json'}
resp = requests.post('http:/xxx/xxx', json=res, headers=headers)上面這樣寫,就不會報錯了。
---- 之前報錯,總結(jié) ----
(1)有些要傳header的,后端接口設(shè)置更多的,header就更復(fù)雜;
(2)之前把res_json = json.dumps()了,然后傳參data=res_json, 后來發(fā)現(xiàn)不行,python調(diào)post不需要!比如報錯的這一句: resp = requests.post('http:/xxx/xxx', data=res_json, headers=headers)
python寫http post請求的四種請求體
HTTP 協(xié)議規(guī)定 POST 提交的數(shù)據(jù)必須放在消息主體(entity-body)中,但協(xié)議并沒有規(guī)定數(shù)據(jù)必須使用什么編碼方式。常見的四種編碼方式如下:
1、application/x-www-form-urlencoded
這應(yīng)該是最常見的 POST 提交數(shù)據(jù)的方式了。瀏覽器的原生 form 表單,如果不設(shè)置 enctype 屬性,那么最終就會以 application/x-www-form-urlencoded 方式提交數(shù)據(jù)。
請求類似于下面這樣(無關(guān)的請求頭在本文中都省略掉了):
POST http://www.example.com HTTP/1.1 ? ?Content-Type: application/x-www-form-urlencoded;charset=utf-8 title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3
2、multipart/form-data
這又是一個常見的 POST 數(shù)據(jù)提交的方式。我們使用表單上傳文件時,必須讓 form 的 enctyped 等于這個值,下面是示例
POST http://www.example.com HTTP/1.1 Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA ------WebKitFormBoundaryrGKCBY7qhFd3TrwA Content-Disposition: form-data; name="text" title ------WebKitFormBoundaryrGKCBY7qhFd3TrwA Content-Disposition: form-data; name="file"; filename="chrome.png" Content-Type: image/png PNG ... content of chrome.png ... ------WebKitFormBoundaryrGKCBY7qhFd3TrwA--
3、application/json
application/json 這個 Content-Type 作為響應(yīng)頭大家肯定不陌生。
實際上,現(xiàn)在越來越多的人把它作為請求頭,用來告訴服務(wù)端消息主體是序列化后的 JSON 字符串。
由于 JSON 規(guī)范的流行,除了低版本 IE 之外的各大瀏覽器都原生支持 JSON.stringify,服務(wù)端語言也都有處理 JSON 的函數(shù),使用 JSON 不會遇上什么麻煩。
4、text/xml
它是一種使用 HTTP 作為傳輸協(xié)議,XML 作為編碼方式的遠(yuǎn)程調(diào)用規(guī)范。
那么Python在調(diào)用外部http請求時,post請求怎么傳請求體呢?說實話樓主只實踐過【1、application/x-www-form-urlencoded】【2、multipart/form-data 】和【3、application/json】
application/x-www-form-urlencoded
import urllib
url = "http://www.example.com"
body_value = {"package": "com.tencent.lian","version_code": "66" } body_value = urllib.urlencode(body_value) request = urllib2.Request(url, body_value) request.add_header(keys, headers[keys]) result = urllib2.urlopen(request ).read()multipart/form-data
需要利用python的poster模塊,安裝poster:pip install poster
代碼:
from poster.encode import multipart_encode?
from poster.streaminghttp import register_openers?
url = "http://www.example.com"
body_value = {"package": "com.tencent.lian","version_code": "66" } register_openers() datagen, re_headers = multipart_encode(body_value) request = urllib2.Request(url, datagen, re_headers) # 如果有請求頭數(shù)據(jù),則添加請求頭 request .add_header(keys, headers[keys]) result = urllib2.urlopen(request ).read()application/json
import json
url = "http://www.example.com"
body_value = {"package": "com.tencent.lian","version_code": "66" } register_openers() body_value = json.JSONEncoder().encode(body_value) request = urllib2.Request(url, body_value) request .add_header(keys, headers[keys]) result = urllib2.urlopen(request ).read()總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
在VSCode中搭建Python開發(fā)環(huán)境并進行調(diào)試
這篇文章介紹了在VSCode中搭建Python開發(fā)環(huán)境并進行調(diào)試的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Python利用pynimate實現(xiàn)制作動態(tài)排序圖
這篇文章主要為大家詳細(xì)介紹了Python如何利用pynimate實現(xiàn)制作動態(tài)排序圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
Python+flask實現(xiàn)restful接口的示例詳解
這篇文章主要為大家詳細(xì)介紹了Python如何利用flask實現(xiàn)restful接口,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以參考一下2023-02-02
python數(shù)字圖像處理環(huán)境安裝與配置過程示例
這篇文章主要為大家介紹了python數(shù)字圖像處理環(huán)境安裝與配置過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Python中try?except語句及實際應(yīng)用詳細(xì)解釋
在Python中try和except是用于異常處理的關(guān)鍵字,它們可以捕獲程序運行時可能出現(xiàn)的錯誤,從而避免程序崩潰,這篇文章主要介紹了Python中try?except語句及實際應(yīng)用的相關(guān)資料,需要的朋友可以參考下2025-04-04

