python寫http post請求的四種請求體

來源:互聯網
上載者:User

標籤:處理   chrome   div   ram   lib   http請求   str   keyword   版本   

 Web自動化測試(25) 

HTTP 協議規定 POST 提交的資料必須放在訊息主體(entity-body)中,但協議並沒有規定資料必須使用什麼編碼方式。常見的四種編碼方式如下: 
1、application/x-www-form-urlencoded 
這應該是最常見的 POST 提交資料的方式了。瀏覽器的原生 form 表單,如果不設定 enctype 屬性,那麼最終就會以 application/x-www-form-urlencoded 方式提交資料。請求類似於下面這樣(無關的要求標頭在本文中都省略掉了):

POST http://www.example.com HTTP/1.1    Content-Type:application/x-www-form-urlencoded;charset=utf-8title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3

2、multipart/form-data 
這又是一個常見的 POST 資料提交的方式。我們使用表單上傳檔案時,必須讓 form 的 enctyped 等於這個值,下面是樣本

POST http://www.example.com HTTP/1.1Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA------WebKitFormBoundaryrGKCBY7qhFd3TrwAContent-Disposition: form-data; name="text"title------WebKitFormBoundaryrGKCBY7qhFd3TrwAContent-Disposition: form-data; name="file"; filename="chrome.png"Content-Type: image/pngPNG ... content of chrome.png ...------WebKitFormBoundaryrGKCBY7qhFd3TrwA--

3、application/json 
application/json 這個 Content-Type 作為回應標頭大家肯定不陌生。實際上,現在越來越多的人把它作為要求標頭,用來告訴服務端訊息主體是序列化後的 JSON 字串。由於 JSON 規範的流行,除了低版本 IE 之外的各大瀏覽器都原生支援 JSON.stringify,服務端語言也都有處理 JSON 的函數,使用 JSON 不會遇上什麼麻煩。

4、text/xml 
它是一種使用 HTTP 作為傳輸協議,XML 作為編碼方式的遠程調用規範。

那麼Python在調用外部http請求時,post請求怎麼傳請求體呢?說實話樓主只實踐過【1、application/x-www-form-urlencoded】【2、multipart/form-data 】和【3、application/json】 
一、application/x-www-form-urlencoded

import urlliburl = "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)# 如果有要求標頭資料,則添加要求標頭request .add_header(keys, headers[keys])result = urllib2.urlopen(request ).read()

二、application/json

import jsonurl = "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()
  

python寫http post請求的四種請求體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.