Python調(diào)用http-post接口的實(shí)現(xiàn)方式
Python調(diào)用http-post接口
- 引入requests包
- 使用post方法,將post請(qǐng)求發(fā)出
- response變量接收接口的返回值
Python通過(guò)post調(diào)用接口報(bào)400
遇到個(gè)坑,簡(jiǎn)單記錄一下!
res = {'a':1, 'b':2} headers = {'content-type': 'application/json'} resp = requests.post('http:/xxx/xxx', json=res, headers=headers)
上面這樣寫(xiě),就不會(huì)報(bào)錯(cuò)了。
---- 之前報(bào)錯(cuò),總結(jié) ----
(1)有些要傳header的,后端接口設(shè)置更多的,header就更復(fù)雜;
(2)之前把res_json = json.dumps()了,然后傳參data=res_json, 后來(lái)發(fā)現(xiàn)不行,python調(diào)post不需要!比如報(bào)錯(cuò)的這一句: resp = requests.post('http:/xxx/xxx', data=res_json, headers=headers)
python寫(xiě)http post請(qǐng)求的四種請(qǐng)求體
HTTP 協(xié)議規(guī)定 POST 提交的數(shù)據(jù)必須放在消息主體(entity-body)中,但協(xié)議并沒(méi)有規(guī)定數(shù)據(jù)必須使用什么編碼方式。常見(jiàn)的四種編碼方式如下:
1、application/x-www-form-urlencoded
這應(yīng)該是最常見(jiàn)的 POST 提交數(shù)據(jù)的方式了。瀏覽器的原生 form 表單,如果不設(shè)置 enctype 屬性,那么最終就會(huì)以 application/x-www-form-urlencoded 方式提交數(shù)據(jù)。
請(qǐng)求類(lèi)似于下面這樣(無(wú)關(guān)的請(qǐng)求頭在本文中都省略掉了):
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
這又是一個(gè)常見(jiàn)的 POST 數(shù)據(jù)提交的方式。我們使用表單上傳文件時(shí),必須讓 form 的 enctyped 等于這個(gè)值,下面是示例
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 這個(gè) Content-Type 作為響應(yīng)頭大家肯定不陌生。
實(shí)際上,現(xiàn)在越來(lái)越多的人把它作為請(qǐng)求頭,用來(lái)告訴服務(wù)端消息主體是序列化后的 JSON 字符串。
由于 JSON 規(guī)范的流行,除了低版本 IE 之外的各大瀏覽器都原生支持 JSON.stringify,服務(wù)端語(yǔ)言也都有處理 JSON 的函數(shù),使用 JSON 不會(huì)遇上什么麻煩。
4、text/xml
它是一種使用 HTTP 作為傳輸協(xié)議,XML 作為編碼方式的遠(yuǎn)程調(diào)用規(guī)范。
那么Python在調(diào)用外部http請(qǐng)求時(shí),post請(qǐng)求怎么傳請(qǐng)求體呢?說(shuō)實(shí)話(huà)樓主只實(shí)踐過(guò)【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) # 如果有請(qǐng)求頭數(shù)據(jù),則添加請(qǐng)求頭 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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python編寫(xiě)網(wǎng)頁(yè)爬蟲(chóng)腳本并實(shí)現(xiàn)APScheduler調(diào)度
爬蟲(chóng)爬的頁(yè)面是京東的電子書(shū)網(wǎng)站頁(yè)面,每天會(huì)更新一些免費(fèi)的電子書(shū),爬蟲(chóng)會(huì)把每天更新的免費(fèi)的書(shū)名以第一時(shí)間通過(guò)郵件發(fā)給我,通知我去下載2014-07-07使用Python實(shí)現(xiàn)ELT統(tǒng)計(jì)多個(gè)服務(wù)器下所有數(shù)據(jù)表信息
這篇文章主要介紹了使用Python實(shí)現(xiàn)ELT統(tǒng)計(jì)多個(gè)服務(wù)器下所有數(shù)據(jù)表信息,ETL,是英文Extract-Transform-Load的縮寫(xiě),用來(lái)描述將數(shù)據(jù)從來(lái)源端經(jīng)過(guò)抽取(extract)、轉(zhuǎn)換(transform)、加載(load)至目的端的過(guò)程,需要的朋友可以參考下2023-07-07Python定時(shí)庫(kù)Apscheduler的簡(jiǎn)單使用
Apscheduler是基于Quartz的Python定時(shí)任務(wù)框架,功能上跟Quartz一致,使用上跟Quartz也幾乎一致。下面通過(guò)本文給大家介紹Python定時(shí)庫(kù)Apscheduler的簡(jiǎn)單使用,感興趣的朋友一起看看吧2021-11-11python flask自定義404錯(cuò)誤頁(yè)面方式
這篇文章主要介紹了python flask自定義404錯(cuò)誤頁(yè)面方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12PyPDF2讀取PDF文件內(nèi)容保存到本地TXT實(shí)例
這篇文章主要介紹了PyPDF2讀取PDF文件內(nèi)容保存到本地TXT實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05如何通過(guò)Python3和ssl實(shí)現(xiàn)加密通信功能
這篇文章主要介紹了如何通過(guò)Python3和ssl實(shí)現(xiàn)加密通信功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python+tkinter使用80行代碼實(shí)現(xiàn)一個(gè)計(jì)算器實(shí)例
這篇文章主要介紹了Python+tkinter使用80行代碼實(shí)現(xiàn)一個(gè)計(jì)算器實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01