Python Make HTTP/JSON Request

July 1, 2019
import requests

url = 'https://jsonplaceholder.typicode.com/todos/1'

r = requests.get(url)
if r.status_code == 200:
    print(r.json())
    # print(r.content)
else:
    print(f"status_code=${r.status_code}")
    # r.raise_for_status()

Custom Headers

headers = {
    'user-agent': 'unknown-crawler/1.0.0'
}

r = requests.get(url, headers=headers)

Get with Query String (params)

payload = {
    'symbol': 'XXX'
}

r = requests.get(url, params=payload)

Post with Body Request (data)

payload = {
    'symbol': 'XXX'
}

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

Post JSON (json)

payload = {
    'symbol': 'XXX'
}

r = requests.post(url, json=payload)

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.