Python: HTTP POST Using Library “requests”


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

“Requests” home page at

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.

here's a sample code using “requests”:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests

r = requests.get('', 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”.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2

gh_url = ''

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)


handler = urllib2.urlopen(req)

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

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

pip install requests

blog comments powered by Disqus