Python Requests – HTTP POST

Python – Send HTTP POST Request

HTTP POST request is used to create or update a resource in a specified server.

In Python Requests library, requests.post() method is used to send a POST request to a server over HTTP. You can also send additional data in the POST request using data parameter.

Examples

1. Send HTTP POST Request

In this example, we shall send a POST request to the server with given URL.

Python Program

import requests

response = requests.post('https://pythonexamples.org/', data = {'key':'value'})
Run Code Copy

requests.post() returns a Response object. It contains all the data and properties like response content, headers, encoding, cookies, etc. Let us print out headers.

Python Program

import requests

response = requests.post('https://pythonexamples.org/',
            data = {'key1':'value1', 'key2':'value2'})
print(response.headers)
Run Code Copy

Output

{'Date': 'Mon, 25 Mar 2019 11:16:23 GMT', 'Content-Type': 'text/html; charset=UTF-8', 'Content-Length': '12136', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=30', 'Server': 'Apache/2', 'X-Powered-By': 'PHP/5.6.30', 'Link': '<https://pythonexamples.org/wp-json/>; rel="https://api.w.org/", <https://pythonexamples.org/>; rel=shortlink', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Referrer-Policy': 'no-referrer-when-downgrade'}

About HTTP POST Request

Following are some of the points to be noted while working with a POST Request.

  • POST requests are never cached
  • POST requests do not remain in the browser history
  • POST requests cannot be bookmarked
  • POST requests have no restrictions on data length

Summary

In this tutorial of Python Examples, we learned how to send a HTTP POST request using Requests library.

Related Tutorials

Code copied to clipboard successfully 👍