urllib - How to send POST request?

ID : 10107

viewed : 98

Tags : urllibpython-2.xhttpliburllib

Top 5 Answer for urllib - How to send POST request?

vote vote

93

If you really want to handle with HTTP using Python, I highly recommend Requests: HTTP for Humans. The POST quickstart adapted to your question is:

>>> import requests >>> r = requests.post("http://bugs.python.org", data={'number': 12524, 'type': 'issue', 'action': 'show'}) >>> print(r.status_code, r.reason) 200 OK >>> print(r.text[:300] + '...')  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title> Issue 12524: change httplib docs POST example - Python tracker  </title> <link rel="shortcut i... >>>  
vote vote

90

This is a solution without any external pip dependencies, but works only in Python 3+ (Python 2 won't work):

from urllib.parse import urlencode from urllib.request import Request, urlopen  url = 'https://httpbin.org/post' # Set destination URL here post_fields = {'foo': 'bar'}     # Set POST fields here  request = Request(url, urlencode(post_fields).encode()) json = urlopen(request).read().decode() print(json) 

Sample output:

{   "args": {},    "data": "",    "files": {},    "form": {     "foo": "bar"   },    "headers": {     "Accept-Encoding": "identity",      "Content-Length": "7",      "Content-Type": "application/x-www-form-urlencoded",      "Host": "httpbin.org",      "User-Agent": "Python-urllib/3.3"   },    "json": null,    "origin": "127.0.0.1",    "url": "https://httpbin.org/post" } 
vote vote

70

You can't achieve POST requests using urllib (only for GET), instead try using requests module, e.g.:

Example 1.0:

import requests  base_url="www.server.com" final_url="/{0}/friendly/{1}/url".format(base_url,any_value_here)  payload = {'number': 2, 'value': 1} response = requests.post(final_url, data=payload)  print(response.text) #TEXT/HTML print(response.status_code, response.reason) #HTTP 

Example 1.2:

>>> import requests  >>> payload = {'key1': 'value1', 'key2': 'value2'}  >>> r = requests.post("http://httpbin.org/post", data=payload) >>> print(r.text) {   ...   "form": {     "key2": "value2",     "key1": "value1"   },   ... } 

Example 1.3:

>>> import json  >>> url = 'https://api.github.com/some/endpoint' >>> payload = {'some': 'data'}  >>> r = requests.post(url, data=json.dumps(payload)) 
vote vote

60

Use requests library to GET, POST, PUT or DELETE by hitting a REST API endpoint. Pass the rest api endpoint url in url, payload(dict) in data and header/metadata in headers

import requests, json  url = "bugs.python.org"  payload = {"number": 12524,             "type": "issue",             "action": "show"}  header = {"Content-type": "application/x-www-form-urlencoded",           "Accept": "text/plain"}   response_decoded_json = requests.post(url, data=payload, headers=header) response_json = response_decoded_json.json()   print(response_json) 
vote vote

51

Your data dictionary conteines names of form input fields, you just keep on right their values to find results. form view Header configures browser to retrieve type of data you declare. With requests library it's easy to send POST:

import requests  url = "https://bugs.python.org" data = {'@number': 12524, '@type': 'issue', '@action': 'show'} headers = {"Content-type": "application/x-www-form-urlencoded", "Accept":"text/plain"} response = requests.post(url, data=data, headers=headers)  print(response.text) 

More about Request object: https://requests.readthedocs.io/en/master/api/

Top 3 video Explaining urllib - How to send POST request?

Related QUESTION?