Let's Run Jinyeah

[Python] Consuming APIs 본문

Programming/Web

[Python] Consuming APIs

jinyeah 2021. 11. 19. 19:45

HTTP library - requests

  • make and send HTTP request to interact with APIs
  • GET / POST / PUT / DELETE
import requests

# GET : url에 특정 id 
api_url = "https://jsonplaceholder.typicode.com/todos/10"
response = requests.get(api_url)
print(response.json())
print(response.status_code)
print(response.headers["Content-Type"]) #metatdata about the response

# POST: 전체 todos url 사용
api_url = "https://jsonplaceholder.typicode.com/todos"
todo = {"userId": 1, "title": "Buy milk", "completed": False}
response = requests.post(api_url, json=todo)
print(response.json())
print(response.status_code) # 201: new resource was created 

# PUT: 특정 id 수정위해 url에 특정 id 지정
api_url = "https://jsonplaceholder.typicode.com/todos/10"
todo = {"userId": 1, "title": "Wash car", "completed": True}
response = requests.put(api_url, json=todo)
print(response.json())
print(response.status_code)

 

'Programming > Web' 카테고리의 다른 글

[FastAPI] Build APIs  (0) 2021.11.19
Build REST APIs  (0) 2021.11.19
REST API  (0) 2021.11.19
[Error]CSS style 변경 후 웹브라우저에 반영이 안되는 경우  (0) 2021.01.15
웹 서버란?  (0) 2021.01.07
Comments