Difference between revisions of "Generate API request with Python"
From MyWiki
(Created page with "https://www.datacamp.com/tutorial/making-http-requests-in-python?dc_referrer=https%3A%2F%2Fwww.google.com%2F<br>") |
|||
Line 1: | Line 1: | ||
https://www.datacamp.com/tutorial/making-http-requests-in-python?dc_referrer=https%3A%2F%2Fwww.google.com%2F<br> | https://www.datacamp.com/tutorial/making-http-requests-in-python?dc_referrer=https%3A%2F%2Fwww.google.com%2F<br> | ||
+ | This uses json:<> | ||
+ | <source lang="python"> | ||
+ | import requests | ||
+ | |||
+ | # The API endpoint | ||
+ | url = "https://jsonplaceholder.typicode.com/posts" | ||
+ | |||
+ | # Data to be sent | ||
+ | data = { | ||
+ | "userID": 1, | ||
+ | "title": "Making a POST request", | ||
+ | "body": "This is the data we created." | ||
+ | } | ||
+ | |||
+ | # A POST request to the API | ||
+ | response = requests.post(url, json=data) | ||
+ | |||
+ | # Print the response | ||
+ | print(response.json()) | ||
+ | </source> |
Revision as of 23:53, 9 December 2024
https://www.datacamp.com/tutorial/making-http-requests-in-python?dc_referrer=https%3A%2F%2Fwww.google.com%2F
This uses json:<>
import requests # The API endpoint url = "https://jsonplaceholder.typicode.com/posts" # Data to be sent data = { "userID": 1, "title": "Making a POST request", "body": "This is the data we created." } # A POST request to the API response = requests.post(url, json=data) # Print the response print(response.json())