REQUESTS Module
Import requests
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get('https://api.github.com/events', params=payload, headers=headers,
stream=False, cookies=cookies, allow_redirects=True, timeout=0.001)
r = requests.post('https://httpbin.org/post', data = payload, json=payload)
r = requests.put('https://httpbin.org/put', data = payload)
r = requests.delete('https://httpbin.org/delete')
r = requests.head('https://httpbin.org/get')
r = requests.options('https://httpbin.org/get')
# json data will be encoded as json automatically,
# it will be ignored if either data or file parameter is passed
Basic¶
Code | Description |
---|---|
r.url |
fully encoded url, i.e. url after encoding of parameters sent with url |
r.text |
content of server's response (after decoding) |
r.encoding |
returns the automatically guessed encoding of the response by requests library |
r.encoding = 'ISO-8859-1' |
change encoding |
r.content |
returns response body as bytes (un-decoded) |
r.status_code |
status code of response (200, 404 etc) |
r.raw r.raw().read(10) # read 10 bytes |
raw socket response from the server, If you want to do this, make sure you set stream=True in parameter body of request |
r.headers |
response headers |
r.cookies['example_cookie_name'] |
returns cookies sent in response |
r.history |
track redirection, we can block resirections by allow_redirects=False parameter in request |
r.raise_for_status() |
will raise an HTTPError if the HTTP request returns an unsuccessful status code |
r.json() |
returns response decoded as a JSON |