<method> <url> <version> <headers> <entity-body> # 例如: # 请求行 POST /wp-admin/admin-ajax.php HTTP/1.1 # 下面都是请求头 Host: zwjjiaozhu.top Content-Length: 69 Accept: */* User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 # 下面是消息主体内容 action=user_login&username=%E5%8F%91&password=+%E5%8F%91&rememberme=1 |
<form method="post" enctype="multipart/form-data" name="myForm" action=“http://www.baidu.com”> <div> <label for="file">Choose a file</label> <input type="file" id="file" name="myFile"> </div> <div> <button type=“submit”>Send form</button> </div> </form> |
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 |
# python脚本 import requests url = "http://httpbin.org/post" data = {"name":"西园公子","age":"666"} headers = {"Content-type":"application/x-www-form-urlencoded"} content = requests.post(url=url,data=data,).text print(content) # 网络请求: POST http://httpbin.org/post HTTP/1.1 Hosthttpbin.org User-Agentpython-requests/2.24.0 Accept-Encodinggzip, deflate Accept*/* Content-Length49 Content-Typeapplication/x-www-form-urlencoded Connectionkeep-alive # 下面是表单内容 name=%E8%A5%BF%E5%9B%AD%E5%85%AC%E5%AD%90&age=666 # 可以看出汉字是使用utf8编码的 # 打印 { "args": {}, "data": "", "files": {}, "form": { # form表单内容 "age": "666", "name": "\u897f\u56ed\u516c\u5b50" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "49", "Content-Type": "application/x-www-form-urlencoded", # Content-Type "Host": "httpbin.org", "User-Agent": "python-requests/2.24.0", # 这里面如果爬虫不设置user-agent的话,本ban的几率很大 "X-Amzn-Trace-Id": "Root=1-60010906-7a51fef342a967cd24c32235" }, "json": null, "origin": "171.90.37.102", "url": "http://httpbin.org/post" } |
# python脚本 import requests from requests_toolbelt import MultipartEncoder m = MultipartEncoder( fields={'field0': 'value1', 'field1': 'value2', 'field2': ('filename', open('data.txt', 'rb'), 'text/plain')} ) content = requests.post('http://httpbin.org/post', data=m, headers={'Content-Type': m.content_type}).text print(content) print(m.content_type) # 1、网络请求: POST http://httpbin.org/post HTTP/1.1 Host: httpbin.org User-Agent: python-requests/2.24.0 Accept-Encoding: gzip, deflate Accept: */* Content-Type: multipart/form-data; boundary=e48c73a7a42e403d868095dc3d060962 Content-Length: 222 Connection: keep-alive # 下面是编码的表单内容 --e48c73a7a42e403d868095dc3d060962 Content-Disposition: form-data; name="field0" value1 --e48c73a7a42e403d868095dc3d060962 Content-Disposition: form-data; name="field1" value2 --e48c73a7a42e403d868095dc3d060962-- Content-Disposition: form-data; name="field2"; filename="filename" Content-Type: text/plain ????¥????è¥???-????-???? --25c88ddc918d40e7a3cd5be0d62476b7-- # 2、打印 { "args": {}, "data": "", "files": { # 发送类型是文件的 "field2": "\u4f60\u597d\uff0c\u897f\u56ed\u516c\u5b50\uff5e" }, "form": { # 发送类型是非文件的,有些类似 urlencode 的消息主体 "field0": "value1", "field1": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "222", "Content-Type": "multipart/form-data; boundary=3123ca302f2c4f0dba1050faa8817ab8", "Host": "httpbin.org", "User-Agent": "python-requests/2.24.0", "X-Amzn-Trace-Id": "Root=1-60017280-15e1a08d69c4611737583c87" }, "json": null, "origin": "191.80.857.122", "url": "http://httpbin.org/post" } multipart/form-data; boundary=3123ca302f2c4f0dba1050faa8817ab8 |
import requests import json url="http://httpbin.org/post" p_data = {"name": "公子哥", "hobby": "coding"} content = requests.post(url, json=json.dumps(p_data), headers={'Content-Type': "application/json"}).text print(content) # 1、原生网络请求 POST /post HTTP/1.1 Host: httpbin.org User-Agent: python-requests/2.24.0 Accept-Encoding: gzip, deflate Accept: */* Content-Type: application/json Content-Length: 62 Connection: keep-alive # 下面是编码成json数据 的表单内容 "{\"name\": \"\\u516c\\u5b50\\u54e5\", \"hobby\": \"coding\"}" # 2、打印数据 { "args": {}, "data": "\"{\\\"name\\\": \\\"\\\\u516c\\\\u5b50\\\\u54e5\\\", \\\"hobby\\\": \\\"coding\\\"}\"", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "62", "Content-Type": "application/json", # 这里指定消息主体编码方式 "Host": "httpbin.org", "User-Agent": "python-requests/2.24.0", "X-Amzn-Trace-Id": "Root=1-6001804b-1c8da9b72910a9e1021e02b3" }, "json": "{\"name\": \"\\u516c\\u5b50\\u54e5\", \"hobby\": \"coding\"}", # 消息主体内容 "origin": "171.10.87.122", "url": "http://httpbin.org/post" } |
import requests # from requests_toolbelt import MultipartEncoder p_data = """ <?xml version="1.0"?> <methodCall> <methodName>examples.getStateName</methodName> <params> <param> <value><i4>41</i4></value> </param> </params> </methodCall> """ content = requests.post(url='http://httpbin.org/post',data=p_data,headers={'Content-Type':'text/xml'}).text print(content) # 2、打印数据 { "args": {}, "data": "\n<?xml version=\"1.0\"?>\n<methodCall>\n <methodName>examples.getStateName</methodName>\n <params>\n <param>\n <value><i4>41</i4></value>\n </param>\n </params>\n</methodCall>\n", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "200", "Content-Type": "text/xml", "Host": "httpbin.org", "User-Agent": "python-requests/2.24.0", "X-Amzn-Trace-Id": "Root=1-60024d51-775180ce108409b413dc68c6" }, "json": null, "origin": "106.33.40.219", "url": "http://httpbin.org/post" } |
p_data = { "name": "西园公子", } |
requests.post(url="http://httpbin.org/post",data=p_data, headers={"Content-type": "application/x-wwww-form-urlencoded"}).json() |
from requests_toolbelt import MultipartEncoder m = MultipartEncoder( fields={'field0': 'value1', 'field1': 'value2', 'field2': ('filename', open('data.txt', 'rb'), 'text/plain')} ) requests.post(url="http://httpbin.org/post",data=m, headers={"Content-type": "multipart/form-data"}).json() |
import json p_data = json.dumps(p_data) requests.post(url="http://httpbin.org/post",json=p_data, headers={"Content-type": "application/json"}).json() |
p_data = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><Request xmlns="http://tempuri.org/"><jme><JobClassFullName>WeChatJSTicket.JobWS.Job.JobRefreshTicket,WeChatJSTicket.JobWS</JobClassFullName><Action>RUN</Action><Param>1</Param><HostIP>127.0.0.1</HostIP><JobInfo>1</JobInfo><NeedParallel>false</NeedParallel></jme></Request></soap:Body></soap:Envelope>' requests.post(url="http://httpbin.org/post",data=p_data, headers={"Content-type": "text/xml"}).json() |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |