Working with http

satya - 1/20/2024, 12:25:07 PM

Standard library reference for request

Standard library reference for request

satya - 1/20/2024, 12:30:26 PM

However the library often used is called "requests"

However the library often used is called "requests"

satya - 1/20/2024, 12:32:57 PM

Requests: http for humans link

Requests: http for humans link

Search for: Requests: http for humans link

satya - 1/20/2024, 12:33:34 PM

Home page for requests module

Home page for requests module

satya - 1/20/2024, 12:34:30 PM

How to install


pip install requests

satya - 1/20/2024, 12:37:09 PM

Request


import requests
r = requests.get(url)

datamap = {key:value, key:value}

r = requests.post(url, data=datamap)

satya - 1/20/2024, 12:40:52 PM

Response


r = requests.post(url, data=datamap)

r.url
r.text
r.encoding
r.content #binary
r.json()
r.status_code
r.raise_for_status()

satya - 1/20/2024, 12:41:05 PM

Examples

Examples

satya - 1/20/2024, 12:42:31 PM

Headers


headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)

satya - 1/20/2024, 12:43:48 PM

Status codes


r.status_code
200 is good
r.raise_for_status()
r.headers

{
    'content-encoding': 'gzip',
    'transfer-encoding': 'chunked',
    'connection': 'close',
    'server': 'nginx/1.0.4',
    'x-runtime': '148ms',
    'etag': '"e1ca502697e5c9317743dc078f67693f"',
    'content-type': 'application/json'
}