Difference between revisions of "Generate API request with Python"

From MyWiki
Jump to: navigation, search
(Created page with "https://www.datacamp.com/tutorial/making-http-requests-in-python?dc_referrer=https%3A%2F%2Fwww.google.com%2F<br>")
 
 
(2 intermediate revisions by the same user not shown)
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>
 +
 +
This is data and not json:<br>
 +
https://apidog.com/blog/python-post-request/<br>
 +
 +
<source lang="python">
 +
import requests
 +
 +
# Define the URL and the data to be sent in the POST request
 +
url = 'http://example.com/test/demo_form.php'
 +
data = {'name1': 'value1', 'name2': 'value2'}
 +
 +
# Make the POST request
 +
response = requests.post(url, data=data)
 +
 +
# Print the status code and the response content
 +
print(f"Status Code: {response.status_code}")
 +
print(f"Response Content: {response.text}")
 +
</source>

Latest revision as of 23:58, 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())

This is data and not json:
https://apidog.com/blog/python-post-request/

import requests
 
# Define the URL and the data to be sent in the POST request
url = 'http://example.com/test/demo_form.php'
data = {'name1': 'value1', 'name2': 'value2'}
 
# Make the POST request
response = requests.post(url, data=data)
 
# Print the status code and the response content
print(f"Status Code: {response.status_code}")
print(f"Response Content: {response.text}")