Postman的Post請求方式的四種類型的資料
阿新 • • 發佈:2018-12-30
- 1. form-data
就是http請求中的multipart/form-data
,它會將表單的資料處理為一條訊息,以標籤為單元,用分隔符分開。既可以上傳鍵值對,也可以上傳檔案。當上傳的欄位是檔案時,會有content-type
來說明檔案型別;content-disposition
用來說明欄位的一些資訊;由於有boundary隔離,所以multipart/form-data
既可以上傳檔案,也可以上傳鍵值對,它採用了鍵值對的方式,所以可以上傳多個檔案。
- 2.x-www-form-urlencoded:
就是application/x-www-from-urlencoded
,會將表單內的資料轉換為鍵值對
- 3.raw
可以上傳任意格式的文字,可以上傳text、json、xml、html等
- 4.binary
相當於content-type:application/octet-stream
,從字面意思得知,只可以上傳二進位制資料,通常用來上傳檔案,由於沒有鍵值,所以,一次只能上傳一個檔案。
python requests 設定headers 和 post請求體x-www-form-urlencoded
1.application/json:是JSON格式提交的一種識別方式。在請求頭裡標示。
2.application/x-www-form-urlencoded : 這是form表單提交的時候的表示方式。
比如我們ajax提交,如果dataType是json,那麼請求頭就是application/json,而我們平常的form提交那麼就是application/x-www-form-urlencoded,自己瀏覽器控制檯看看就知道了。
3.multipart/form-data:這又是一個常見的 POST 資料提交的方式。我們使用表單上傳檔案時,必須讓 form 的 enctyped 等於這個值
4.text/xml :它是一種使用 HTTP 作為傳輸協議,XML 作為編碼方式的遠端呼叫規範
#coding:utf-8
from fake_useragent import UserAgent;
import requests
ua=UserAgent()
headers={
"Proxy-Connection": "keep-alive",
"Pragma": "no-cache",
# "DNT":"1",
"User-Agent":ua.random,
"Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4",
"Referer": "www.huixiaoer.com",
"Accept-Charset": "gb2312,gbk;q=0.7,utf-8;q=0.7,*;q=0.7",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate, sdch",
"Cache-Control":"max-age=0",
"Connection":"keep-alive",
"Content-Type":"application/x-www-form-urlencoded",
"Host":"www.huixiaoer.com",
"Upgrade-Insecure-Requests":"1",
"X-Requested-With":"XMLHttpRequest"
}
cookies={
"pgv_pvi":"9755294720",
"aliyungf_tc":"AQAAAIEbHSUhVA4ATkxVeHH7o+UfJUCq",
"acw_tc":"AQAAAFesTD4BeQ4ATkxVeNrQ4zX/wI03",
"PHPSESSID":"flc3hhtdbcgvr4pgekhvk7rrb1",
"pgv_si":"s7094364160",
"city":"440100",
"_ga":"GA1.2.968334469.1525633526",
"_gid":"GA1.2.1982983394.1525633526",
"sensorsdata2015jssdkcross":"%7B%22distinct_id%22%3A%22162fc16d967476-00a0ee49af4a81-5d4e211f-1049088-162fc16d9684e5%22%2C%22%24device_id%22%3A%22162fc16d967476-00a0ee49af4a81-5d4e211f-1049088-162fc16d9684e5%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_utm_source%22%3A%22bdpp_web%22%2C%22%24latest_utm_medium%22%3A%22ppc%22%2C%22%24latest_utm_campaign%22%3A%22title_20180223%22%7D%7D",
"Hm_lvt_d47d0c2743e9b14d07c86e077d6bdaa2":"1524647779,1524651322,1525633526,1525673504",
"Hm_lpvt_d47d0c2743e9b14d07c86e077d6bdaa2":"1525674666"
}
datas={
'conditions':'{"city_code":"440100","hid":-1,"capacity":-1,"type_code":-1,"tag":-1,"keyword":-1,"key":-1,"lat":"0","lng":"0","center_name":-1,"has_package":-1,"has_special":0,"has_conference":1,"order":-1,"dur":-1,"bud":-1,"page":1,"num":"","cap_num":"","qt":0}'
}
url="http://www.huixiaoer.com/so-api/ajax-get-so-data"
session=requests.session()
requ=session.post(url,data=datas,headers=headers,cookies=cookies)
res=requ.text
print(res)