forked from atilsamancioglu/P13-RequestsExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
20 lines (16 loc) · 724 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import requests
import json
#GET
user_input = input("enter id: ")
get_url = f"https://jsonplaceholder.typicode.com/todos/{user_input}"
get_response = requests.get(get_url)
print(get_response.json())
#POST
to_do_item = {"userId": 2, "title": "my to do", "completed": False}
post_url = "https://jsonplaceholder.typicode.com/todos"
#optional header - it works without header as well. to test how it works, you can do the following:
headers = {"Content-Type": "application/json"}
#post_response = requests.post(post_url, json=to_do_item, headers=headers)
#if you need to convert dict into json to send as data
post_response = requests.post(post_url, data=json.dumps(to_do_item), headers=headers)
print(post_response.json())