Python Interface Automation Test (ii)-requests.post ()

Source: Internet
Author: User

The previous section describes the basic use of the Requests.get () method, and this section describes the use of the Requests.post () method:

This article directory:

I. Definition of methods

Second, the Post method is simple to use

1. Post with Data

2. Post with header

3. Post with JSON

4, with the parameter of the post

5, ordinary file upload

6. Customized File Upload

7, multi-File upload

First, the method definition:

1, to the official document went to the next requests.post () method definition, as follows:

2. Source code:

3, commonly used return information:

Second, the Post method is simple to use:

1. Post with data:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "post" url = '. Join ([Host,endpoint ] data = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}r = Requests.post (url,data=data) #response = R.json () print (R.text)

Output:

{  "args": {},   "Data": "",   "files": {},   "form": {    "Key1": "Value1", "     Key2": "Value2"  },   "Headers": {    "Accept": "*/*",     "accept-encoding": "gzip, deflate",     "Connection": "Close",     " Content-length ":", "     content-type": "application/x-www-form-urlencoded",     "Host": "httpbin.org",     "user-agent": "python-requests/2.18.1"  },   "JSON": null,   "origin": "183.14.133.88",   "url ":" Http://httpbin.org/post "}

2. Post with header:

#-*-Coding:utf-8-*-import requestsimport JSON
Host = "http://httpbin.org/" endpoint = "post" url = '. Join ([host,endpoint]) headers = {"User-agent": "Test request headers "}# r = Requests.post (URL) r = requests.post (url,headers=headers) #response = R.json ()

Output:

{  "args": {},   "Data": "",   "files": {}, "   form": {},   "headers": {    "Accept": "*/*",     " Accept-encoding ":" gzip, deflate ",     " Connection ":" Close ",     " content-length ":" 0 ",     " Host ":" Httpbin.org " ,     "user-agent": "Test Request Headers"  },   "JSON": null,   "origin": "183.14.133.88",   "url": " Http://httpbin.org/post "}

3. Post with JSON:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "POST"
url = ". Join ([host,endpoint]) data = { " sites ": [ {" name ":" Test "," url ":" Www.test.com "}, {" name ":" Google "," url ":" www.google.com "}, {" name ":" Weibo "," url ":" Www.weibo.com "} ]}r = Requests.post (url,json=data) # r = r Equests.post (Url,data=json.dumps (data)) response = R.json ()

Output:

{  "args": {},   "data": "{\" sites\ ": [{\" url\ ": \" www.test.com\ ", \" name\ ": \" Test\ "}, {\" url\ ": \" Www.google.com\ ", \" name\ ": \" Google\ "}, {\" url\ ": \" www.weibo.com\ ", \" name\ ": \" Weibo\ "}]}", "   Files": {},   " Form ": {},   " headers ": {    " Accept ":" */* ",     " accept-encoding ":" gzip, deflate ",     " Connection ":" Close ",     "Content-length": "$",     "Content-type": "Application/json",     "Host": "httpbin.org",     " User-agent ":" python-requests/2.18.1 "  },   " JSON ": {    " sites ": [      {        " name ":" Test ",         " url ":" Www.test.com "      },       {        " name ":" Google ",         " url ":" Www.google.com "      },       {        " name ":" Weibo ",         " url ":" Www.weibo.com "      }    ]  },   " origin ":" 183.14.133.88 ",   " url ":"// Httpbin.org/post "}

4. Post with parameters:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "post" url = '. Join ([Host,endpoint ] params = {' Key1 ': ' params1 ', ' key2 ': ' params2 '}# r = requests.post (URL) r = requests.post (url,params=params) #response = R.json () print (R.text)

Output:

{"  args": {    "Key1": "Params1",     "Key2": "Params2"  },   "Data": "",   "files": {},   "form": {},   "Headers": {    "Accept": "*/*",     "accept-encoding": "gzip, deflate",     "Connection": "Close",     " Content-length ":" 0 ","     Host ":" httpbin.org ",     " user-agent ":" python-requests/2.18.1 "  },   " JSON " : null,   "origin": "183.14.133.88",   "url": "Http://httpbin.org/post?key2=params2&key1=params1"}

5, ordinary file upload:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "POST"
url = '. Join ([host,endpoint]) #普通上传files = { ' file ': Open (' test.txt ', ' RB ') }r = Requests.post (url,files=files ) Print (R.text)

Output:

{  "args": {},   "Data": "",   "files": {    "file": "Hello world!\n"  },   "form": {},   "headers" : {    "Accept": "*/*",     "accept-encoding": "gzip, deflate",     "Connection": "Close",     "content-length ":" 157 ","     content-type ":" Multipart/form-data; boundary=392865f79bf6431f8a53c9d56c62571e ",     " Host ":" httpbin.org ",     " user-agent ":" python-requests/ 2.18.1 "  },   " JSON ": null,   " origin ":" 183.14.133.88 ",   " url ":" Http://httpbin.org/post "}

6. Customized file upload:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "post" url = '. Join ([Host,endpoint ]) #自定义文件名, file type, request header files = {        ' file ':(' test.png ', open (' test.png ', ' RB '), ' image/png ')}r = Requests.post (url,files= files) print (R.text) heman793

Output comparison in, will not post.

7. Upload Multiple files:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "post" url = '. Join ([Host,endpoint ]) #多文件上传files = [    (' File1 ', (' Test.txt ', open (' test.txt ', ' RB ')),    (' File2 ', (' Test.png ', open (' test.png ', ' RB '))    ]r = Requests.post (url,files=files) print (R.text)

Output, too much content, no post.

8. Streaming Upload:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "post" url = '. Join ([Host,endpoint ]) #流式上传with open (' Test.txt ') as f:    r = requests.post (Url,data = f) Print (R.text)

Output:

{  "args": {},   "data": "Hello world!\n", "   Files": {}, "   form": {},   "headers": {    "Accept": "*/*" ,     "accept-encoding": "gzip, deflate",     "Connection": "Close",     "content-length": "+",     "Host": " Httpbin.org ",     " user-agent ":" python-requests/2.18.1 "  },   " JSON ": null,   " origin ":" 183.14.133.88 ",   " url ":" Http://httpbin.org/post "}

Python Interface Automation Test (ii)-requests.post ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.