Requests

Table of Contents

Overview

import requests
r = requests.get('http://httpbin.org/get', params={'key':'value'})
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
r = requests.put('http://httpbin.org/put', data = {'key':'value'})
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')
r.url
r.status_code
r.raise_for_status()  # raises requests.HTTPError
r.json()              # raises ValueError
r.text
r.raw
r.encoding
r.history

requests.request(method, url, **kwargs) reference

Parameters

data

Accepts data in forms of {} or [(k, v)]

json

Like data, but the payload is automatically json encoded, and Content-Type is set to application/json

headers

{}, all values must be in string-like types

auth

a tuple of (<type>, <credentials>). Overrides headers['Authorization']

requests.Response reference

Members

text
content in unicode
coookies
status_code
like 200
ok
True if status_code < 400
headers
json(**kwargs)
kwargs: Optional arguments that json.loads takes.

requests.Session reference

s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('http://httpbin.org/cookies')

print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'
s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})

# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
# Make sure to close the session after use
with requests.Session() as s:
    s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')

requests.Request reference

from requests import Request, Session

s = Session()
req = Request('GET',  url, data=data, headers=headers)

prepped = s.prepare_request(req)

# do something with prepped.body
prepped.body = 'Seriously, send exactly these bytes.'

# do something with prepped.headers
prepped.headers['Keep-Dead'] = 'parrot'

resp = s.send(prepped,
    stream=stream,
    verify=verify,
    proxies=proxies,
    cert=cert,
    timeout=timeout
)

print(resp.status_code)

Download an image with requests howto

Use Response.content: