Python: HTTP POST Using Library “requests”

By Xah Lee. Date: . Last updated: .
from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = 'http://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)

# output

# {
#   "args": {},
#   "data": "",
#   "files": {},
#   "form": {
#     "foo": "bar"
#   },
#   "headers": {
#     "Accept-Encoding": "identity",
#     "Connection": "close",
#     "Content-Length": "7",
#     "Content-Type": "application/x-www-form-urlencoded",
#     "Host": "httpbin.org",
#     "User-Agent": "Python-urllib/3.6"
#   },
#   "json": null,
#   "origin": "172.10.232.224",
#   "url": "http://httpbin.org/post"
# }

Using Package Requests

Requests is a easy-to-use HTTP library, for Python.

“Requests” home page at

http://docs.python-requests.org/en/latest/

Python's standard urllib2 module provides most of the HTTP capabilities you need, but the API is thoroughly broken. It was built for a different time — and a different web. It requires an enormous amount of work (even method overrides) to perform the simplest of tasks.

install the package.

pip install requests

Here's a sample code using “requests”:

# -*- coding: utf-8 -*-
# python 2

import requests

r = requests.get('https://api.github.com', auth=('user', 'pass'))

print r.status_code
print r.headers['content-type']

# ------
# 200
# 'application/json'

Here's a sample code using standard Python lib “urllib2”.

# -*- coding: utf-8 -*-

import urllib2

gh_url = 'https://api.github.com'

req = urllib2.Request(gh_url)

password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, gh_url, 'user', 'pass')

auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)

urllib2.install_opener(opener)

handler = urllib2.urlopen(req)

print handler.getcode()
print handler.headers.getheader('content-type')

# ------------------------------
# output

# 200
# 'application/json'