POST請(qǐng)求用于向服務(wù)器提交數(shù)據(jù)席覆,比如增刪改數(shù)據(jù)直砂,提交一個(gè)表單新建一個(gè)用戶(hù)菌仁、或修改一個(gè)用戶(hù)等。
對(duì)于POST請(qǐng)求哆键,我們可以通過(guò)瀏覽器開(kāi)發(fā)者工具或者其他外部工具來(lái)進(jìn)行抓包掘托,得到請(qǐng)求的URL、請(qǐng)求頭(request headers)以及請(qǐng)求的表單data信息籍嘹,這三樣恰恰是我們用Requests模擬POST請(qǐng)求時(shí)需要的闪盔。
關(guān)于請(qǐng)求頭的配置和GET請(qǐng)求是一樣的弯院,都是定義headers
屬性即可。
而關(guān)于POST請(qǐng)求提交的參數(shù)泪掀,是和GET請(qǐng)求是不一樣的听绳。
post請(qǐng)求四種傳送正文方式:
(1)請(qǐng)求正文是application/x-www-form-urlencoded
(2)請(qǐng)求正文是multipart/form-data
(3)請(qǐng)求正文是raw
(4)請(qǐng)求正文是binary
這四種提交數(shù)據(jù)的方式,是在請(qǐng)求頭Content-Type
屬性中來(lái)定義异赫。
1椅挣、application/x-www-form-urlencoded
Reqeusts支持以application/x-www-form-urlencoded
數(shù)據(jù)格式發(fā)送POST請(qǐng)求(標(biāo)準(zhǔn)的POST請(qǐng)求數(shù)據(jù)格式,默認(rèn))塔拳,只需要將請(qǐng)求的參數(shù)構(gòu)造成一個(gè)字典鼠证,然后傳給requests.post()
的data參數(shù)即可。
示例:
"""
1.學(xué)習(xí)目標(biāo)
必須掌握requests庫(kù)發(fā)送post請(qǐng)求方法
2.HTTP協(xié)議中post請(qǐng)求參數(shù)類(lèi)型
requests.post(url, data=None, json=None, **kwargs)
根據(jù)不同的請(qǐng)求參數(shù)類(lèi)型分為如下幾種:
x-www-form-data-urlencoded
raw_json格式
form-data
binary
3.json格式
# 1.導(dǎo)入requests庫(kù)
# 2.明確請(qǐng)求地址
# 3.明確請(qǐng)求參數(shù)
data = {key:value} 字典格式
# 4.發(fā)送請(qǐng)求
requests.post(url=url,json=data)
4.需求
通過(guò)訪(fǎng)問(wèn)http://httpbin.org/post接口靠抑,驗(yàn)證post參數(shù)類(lèi)型
"""
# 1.導(dǎo)入requests庫(kù)
import requests
import json
# 2.明確請(qǐng)求地址
url = "http://httpbin.org/post"
# 3.明確請(qǐng)求參數(shù)
data = {
"dep_id": "T01",
"dep_name": "Test學(xué)院",
"master_name": "Test-Master",
"slogan": "Here is Slogan"
}
# 4.發(fā)送請(qǐng)求
response = requests.post(url=url, data=data)
# 將python對(duì)象轉(zhuǎn)換為json字符串(格式化返回?cái)?shù)據(jù))
result = json.dumps(response.json(), indent=2, ensure_ascii=False)
# print(type(result)) # 字符串類(lèi)型
print(result)
"""
返回結(jié)果:
{
"args": {},
"data": "",
"files": {},
****************主要看這里
"form": {
"dep_id": "T01",
"dep_name": "Test學(xué)院",
"master_name": "Test-Master",
"slogan": "Here is Slogan"
},
***********************
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "88",
*****************主要看這里
"Content-Type": "application/x-www-form-urlencoded",
*****************
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4",
"X-Amzn-Trace-Id": "Root=1-5ff401e3-1553596b7788e77e275c4772"
},
"json": null,
"origin": "106.35.9.12",
"url": "http://httpbin.org/post"
}
"""
說(shuō)明:
- 發(fā)送的請(qǐng)求中量九,form屬性接收了參數(shù)。
- 在請(qǐng)求頭中颂碧,
Content-Type
屬性為application/x-www-form-urlencoded
- 使用
application/x-www-form-urlencoded
格式發(fā)送數(shù)據(jù)荠列,requests.post(url=url, data=data)
方法中一定要使用data變量來(lái)接收參數(shù)。 - 換句話(huà)說(shuō)數(shù)據(jù)格式是字典格式载城,使用data變量來(lái)接收肌似,會(huì)默認(rèn)發(fā)送
application/x-www-form-urlencoded
數(shù)據(jù)格式的POST請(qǐng)求。(也可以在請(qǐng)求頭中明確一下Content-Type
屬性诉瓦,但沒(méi)必要川队。)
2、請(qǐng)求正文是raw
RAW的原意就是“未經(jīng)加工”垦搬。換句話(huà)說(shuō)RAW方式使用的是純字符串的數(shù)據(jù)上傳方式呼寸,所以在發(fā)送POST請(qǐng)求之前,可能需要手工的把一些JSON格式的數(shù)據(jù)轉(zhuǎn)換成字符串的(加兩單引號(hào))猴贰,在進(jìn)行提交。
RAW數(shù)據(jù)格式的POST請(qǐng)求有兩種:
- 一種是xml格式文本(text/xml)
- 一種是json格式文本(application/json)
下面我們一一說(shuō)明:
(1)json格式文本(application/json)
# 1.導(dǎo)入requests庫(kù)
import requests
import json
# 2.明確請(qǐng)求地址
url = "http://httpbin.org/post"
# 3.明確請(qǐng)求參數(shù)
data = {
"data": [
{
"dep_id": "T01",
"dep_name": "Test學(xué)院",
"master_name": "Test-Master",
"slogan": "Here is Slogan"
}
]
}
# headers = {"Content-Type": "application/json"}
# 4.發(fā)送請(qǐng)求
response = requests.post(url=url, json=data)
print(response) # <Response [200]>
print(response.text)
"""
返回結(jié)果:
{
"args": {},
"data": "{\"data\": [{\"dep_id\": \"T01\", \"dep_name\": \"Test\\u5b66\\u9662\", \"master_name\": \"Test-Master\", \"slogan\": \"Here is Slogan\"}]}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "119",
**************************主要看這里
"Content-Type": "application/json",
**************************
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4",
"X-Amzn-Trace-Id": "Root=1-5ff40a9d-6a6f19d272ba4c1b40ff7bbb"
},
**************************主要看這里
"json": {
"data": [
{
"dep_id": "T01",
"dep_name": "Test\u5b66\u9662",
"master_name": "Test-Master",
"slogan": "Here is Slogan"
}
]
},
**************************
"origin": "106.35.9.12",
"url": "http://httpbin.org/post"
}
"""
說(shuō)明:
- 發(fā)送的請(qǐng)求中河狐,json屬性接收了參數(shù)米绕。
- 在請(qǐng)求頭中,
Content-Type
屬性為application/json
馋艺。 - 使用
application/json
格式發(fā)送數(shù)據(jù)栅干,requests.post(url=url, json=data)
方法中一定要使用json變量來(lái)接收參數(shù)。 - 換句話(huà)說(shuō)數(shù)據(jù)格式是Json格式捐祠,使用json變量來(lái)接收碱鳞,Requests會(huì)默認(rèn)發(fā)送
application/json
數(shù)據(jù)格式的POST請(qǐng)求。(也可以在請(qǐng)求頭中明確一下Content-Type
屬性踱蛀,但沒(méi)必要窿给。)
注意:
這里我們可以發(fā)現(xiàn)Requests模擬post請(qǐng)求時(shí)贵白,請(qǐng)求頭格式為application/x-www-form-urlencoded與application/json的主要差別在于請(qǐng)求主體的構(gòu)造格式(前者是鍵值對(duì),后者是JSON串),前者直接用字典傳入崩泡,后者用json.dumps()函數(shù)將字典轉(zhuǎn)為JSON串即可禁荒。
也就是說(shuō)在有需要的時(shí)候json模塊下的dumps函數(shù)可以將dict轉(zhuǎn)換為str。
(2)xml格式文本(text/xml)
# 1.導(dǎo)入requests庫(kù)
import requests
import json
# 2.明確請(qǐng)求地址
url = "http://httpbin.org/post"
# 3.明確請(qǐng)求參數(shù)
data = '<sites>' \
'<site>' \
'<name>菜鳥(niǎo)教程</name>' \
'<url>www.runoob.com</url>' \
'</site>' \
'<site>' \
'<name>Google</name>' \
'<url>www.google.com</url>' \
'</site>' \
'</sites>'
# requests.post方法中適用json變量來(lái)接收數(shù)據(jù)角撞,
# 默認(rèn)是"Content-Type": "application/json",
# 這里我們需要重新聲明一下Content-Type屬性呛伴。
headers = {'Content-type': 'text/xml'}
# 4.發(fā)送請(qǐng)求
# 如果數(shù)據(jù)用data變量來(lái)接收會(huì)報(bào)錯(cuò)。
response = requests.post(url=url, json=data, headers=headers)
print(response) # <Response [200]>
# print(response.text)
# 將python對(duì)象轉(zhuǎn)換為json字符串(格式化返回?cái)?shù)據(jù))
result = json.dumps(response.json(), indent=2, ensure_ascii=False)
# print(type(result)) # 字符串類(lèi)型
print(result)
"""
返回結(jié)果:
{
"args": {},
"data": "\"<sites><site><name>\\u83dc\\u9e1f\\u6559\\u7a0b</name><url>www.runoob.com</url></site><site><name>Google</name><url>www.google.com</url></site></sites>\"",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "149",
**************************主要看這里
"Content-Type": "text/xml",
**************************
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4",
"X-Amzn-Trace-Id": "Root=1-5ff40fa5-21a79b532b1ccf6d20173fd7"
},
**************************主要看這里
"json": "<sites><site><name>菜鳥(niǎo)教程</name><url>www.runoob.com</url></site><site><name>Google</name><url>www.google.com</url></site></sites>",
**************************
"origin": "106.35.9.12",
"url": "http://httpbin.org/post"
}
"""
說(shuō)明:
- text/xml格式相對(duì)用的少谒所。
- xml也可以作為一個(gè)文件來(lái)傳輸热康。
- 需要重新聲明請(qǐng)求頭中
Content-Type
屬性。 - 其他和
application/json
一樣劣领。
提示:其實(shí)raw格式數(shù)據(jù)可以上傳text姐军、json、xml剖踊、html等純字符的文本庶弃。