Requests
By: Prijesh Dholaria, Zahabiya Rangwala,
Komal Kishore Dave, Oluwatoyin Alabede,
Ronald Munoz
Introduction to Requests in Python
Requests - HTTP library for Python that allows to easily make HTTP requests to get information from
websites.
A request is made up of – Endpoint, Method, Data and Header
To use requests library, use import requests
Example: r = requests.get('https://api.github.com/events')
Here,
r is the response object.
Requests in python support all HTTP methods
1. Get -> r = requests.get('https://httpbin.org/get')
2. Post -> r = requests.post('https://httpbin.org/post', data={'key': 'value'})
3. Put -> r = requests.put('https://httpbin.org/put', data={'key': 'value'})
4. Delete -> r = requests.delete('https://httpbin.org/delete')
2
Response Content
A response is received from the API after a request has been made. It will
have a header and response data. The response header consists of useful
metadata about the response, while the response data returns what was
requested.
Example: r = requests.get('https://api.github.com/events') with r as the
response object.
When you print r
print(r) >>>Response<200>
When this code example prints the response object to the console, it
returns the name of the object’s class and the status code the request
returned.
You can also print
response.content() which returns the the raw bytes of the data payload
response.text() Returns a string representation of the data payload
response.json() API returns JSON.
3
Binary/ JSON/ RAW Response Content
Binary data - unit can take on only Requests have built in JSON To get raw socket response from
two possible states, as 0 and 1. decoder server, you can access the
Response body can also be It’s used for transmitting data in response with r.raw as one of
accessed as bytes, for non-text web applications (e.g., sending the functions to get raw content.
requests:
some data from the server to Response.raw is a raw stream of
>>> r.content the client, so it can be displayed bytes - it does not transform the
on a web page, or vice versa response content
b'[{"repository":{"open_issues":0,"u
rl":"https://github.com/...
Eg: Create an image from binary Eg: >>> import requests >>> r = requests.get
data returned by a request: >>> r = requests.get ('https://api.github.com/events',
>>> from PIL import Image ('https://api.github.com/events') stream=True) >>> r.raw
>>> from io import BytesIO >>> r.json() <urllib3.response.HTTPResponse
>>> i = [{'repository': {'open_issues': 0, object at 0x101194810> >>>
Image.open(BytesIO(r.content)) 'url': 'https://github.com/... r.raw.read(10) 4
More Complicated POST Requests
>>> payload = {'key1': 'value1',
Send form-encoded data like an HTML form. 'key2': 'value2'}
Pass a dictionary to the data argument and dictionary of data will >>> r =
automatically be form-encoded requests.post("https://httpbin.or
The data argument can also have multiple values for each key. This can
g/post", data=payload)
be done by making data by either of the following: >>> print(r.text)
--------------------------------------
1. List of tuples payload_tuples = [('key1', 'value1'), ('key1', 'value2')] {
2. Dictionary with lists as values payload_dict = {'key1': ['value1', ...
'value2']} "form": {
"key2": "value2",
If non encoded data has to be auto encoded -> use JSON parameter "key1": "value1"
>>> payload = {'some': 'data'} },
...
>>> r = requests.post(url, json=payload) }
5
Response Headers and status codes
Response Header: This type of headers contains Snippet
the location of the source that has been
requested by the client or about the server >>> r.headers
providing the information. {
'content-encoding': 'gzip',
They can be viewed using Python dictionary 'transfer-encoding': 'chunked',
'connection': 'close',
>>> r = requests.get('https://httpbin.org/get') 'server': 'nginx/1.0.4',
'x-runtime': '148ms',
>>> r.status_code 'etag':
200 '"e1ca502697e5c9317743dc078f67693f"',
'content-type': 'application/json'
If we made a bad request (a 4XX client error or }
5XX server error response)
6
Cookies
Cookies are created in every website you visit to identify you.
They also speed up some authentication parts of the website by saving
your information for that website.
One can access the cookies from a website using requests.
Cookies can be returned in a dictionary format from RequestsCookieJar.
Which allows for more complete interface, suitable for multiple domains.
7
Errors and Exceptions
In the event of a network problem (e.g. DNS failure, refused connection, etc) -> Requests raises
a ConnectionError exception.
HTTP request returned an unsuccessful status code -> Response.raise_for_status() raises an
HTTPError.
Request time out -> Timeout exception is raised.
Request exceeds the configured number of maximum redirections -> TooManyRedirects
exception is raised.
All exceptions that Requests explicitly raises inherit from
requests.exceptions.RequestException
8
THANK YOU