Python uses requests to send the POST request instance code, pythonrequests

Source: Internet
Author: User

Python uses requests to send the POST request instance code, pythonrequests

This article focuses on Python's use of requests to send POST requests. The details are as follows.

An http request consists of three parts: the request line, request header, and message body, similar to the following:

  • Request Line
  • Request Header
  • Message Body

The HTTP Protocol specifies that the data submitted by post must be placed in the message body, but the Protocol does not specify the encoding method. The server uses the Content-Type field in the request header to determine the encoding method of the message body in the request, and then parse the message body. The specific encoding methods include:

application/x-www-form-urlencoded

The most common method of data submission by post is to submit data in form.

application/json

Submit data in json strings.

multipart/form-data

Generally used to upload files.

2.7.1 send post request in form

Reqeusts supports sending post requests in form. You only need to construct the request parameters into a dictionary and then pass it to the data parameter of requests. post.

url = 'http://httpbin.org/post'd = {'key1': 'value1', 'key2': 'value2'}r = requests.post(url, data=d)print r.text

Output:

{ “args”: {}, “data”: “”, “files”: {}, “form”: { “key1”: “value1”, “key2”: “value2” }, “headers”: { …… “Content-Type”: “application/x-www-form-urlencoded”, …… }, “json”: null, …… }

The Content-Type field in the Request Header has been set to application/x-www-form-urlencoded, and d = {'key1': 'value1 ', 'key2 ': 'value2'} is submitted to the server as a form. The form field returned by the server is the submitted data.

2.7.2 send a post request in json format

You can send a json string to the data parameter of requests. post,

url = 'http://httpbin.org/post's = json.dumps({'key1': 'value1', 'key2': 'value2'})r = requests.post(url, data=s)print r.text

Output:

{ “args”: {}, “data”: “{\”key2\”: \”value2\”, \”key1\”: \”value1\”}”, “files”: {}, “form”: {}, “headers”: { …… “Content-Type”: “application/json”, …… }, “json”: { “key1”: “value1”, “key2”: “value2” }, …… }

You can see that the Content-Type of the request header is set to application/json, And the json string s is submitted to the server.

2.7.3 send a post request in the form of multipart

Requests also supports sending post requests in the form of multipart. You only need to pass a file to the files parameter of Requests. post.

url = 'http://httpbin.org/post'files = {'file': open('report.txt', 'rb')}r = requests.post(url, files=files)print r.text

Output:

{ “args”: {}, “data”: “”, “files”: { “file”: “Hello world!” }, “form”: {}, “headers”: {…… “Content-Type”: “multipart/form-data; boundary=467e443f4c3d403c8559e2ebd009bf4a”, …… }, “json”: null, …… 

The statement file report.txt contains only one line: Hello world !, From the response result, we can see that the data has been uploaded to the server.

Summary

The above is all the content of the POST request instance code sent by Python using requests. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.