Skip to content

Instantly share code, notes, and snippets.

@1f604
Last active January 28, 2025 23:06
Show Gist options
  • Save 1f604/eef41e408461d547fe0f0f748353d17e to your computer and use it in GitHub Desktop.
Save 1f604/eef41e408461d547fe0f0f748353d17e to your computer and use it in GitHub Desktop.
requests demo
import requests
from typing import Optional
def send_get_request(url, params:Optional[dict]=None) -> requests.Response:
return requests.get(url, params=params)
def send_post_request(url, data_dict:dict) -> requests.Response:
return requests.post(url=url, json=data_dict)
GET_API_ENDPOINT = "https://jsonplaceholder.typicode.com/posts/1"
POST_API_ENDPOINT = "https://jsonplaceholder.typicode.com/posts"
### A GET request to the API ###
# 1. Send the request
resp:requests.Response = send_get_request(GET_API_ENDPOINT)
# 2. Print the response
print("GET response status code:", resp.status_code)
print("GET response data:", resp.json())
print("==================================================")
### A POST request to the API##
post_data:dict = {
"userID": 1,
"id": 1,
"title": "Making a POST request",
"body": "This is the data we created."
}
# 1. Send the request
resp = send_post_request(POST_API_ENDPOINT, post_data)
# 2. Print the response
print("POST response status code:", resp.status_code)
print("POST response data:", resp.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment