Python Make HTTP/JSON Request

Jul 1, 2019
import requestsurl = '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:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.