Python POST request

Last Updated On Thursday 20th Jan 2022

The HTTP protocol allows you to send and receive information through the web including webpages, images, and other web resources.

Request in Python

  • Requests is a python Library that allows you to send HTTP/1.1 requests easily.
	import requests
	

Post Requests

  • POST is the most common Request method used to send data mostly through ‘form’ to the server for creating/updating in the server.
  • POST request sends the data in a request body.
	url_post='http://httpbin.org/post'
	
  • To make a POST request we use the post() function.
  • The variable payload is passed to the parameter data.
	r_post=requests.post(url_post,data=payload)
	

Make a Request

	r = requests.post('http://httpbin.org/post')
print(r.text)
	
	{
  "args": {}, 
  "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": "185.136.79.217", 
  "url": "http://httpbin.org/post"
}
	

Passing Parameters

	payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)
print(r.text)
	
	{
  "args": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.1"
  }, 
  "origin": "185.136.79.217", 
  "url": "http://httpbin.org/get?key1=value1&key2=value2"
}