0% found this document useful (0 votes)
11 views1 page

HTTP Requests

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

HTTP Requests

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Communicating with APIs involves making HTTP (Hypertext Transfer Protocol)

requests. The most common types of HTTP requests are GET (retrieve data), POST
(send data), PUT (update data), and DELETE (remove data).

To make HTTP requests in Python, we can use the built-in requests module.
Making a GET Request 🧲

A GET request retrieves data from a web server. Here’s how you can make a GET
request to a sample API:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts')
# Let's print the status code
print(response.status_code) # 200 means success!
# Now, let's print the data
print(response.json())

In this example, https://jsonplaceholder.typicode.com/posts it is the API endpoint


we’re making the GET request to.

The response.json() method returns the data we received from the API in JSON
format.
Making a POST Request 📤

A POST request sends data to a web server. Here’s how you can make a POST request:
import requests
data = {
"title": "foo",
"body": "bar",
"userId": 1
}
response = requests.post('https://jsonplaceholder.typicode.com/posts', data=data)
# Print the status code
print(response.status_code) # 201 means created!
# Print the response data
print(response.json())

In this example, the data dictionary contains the data we want to send to the API.

No Exercise Needed – For Now! 🙌

Hang tight, coding comrades! 🖖 No need for exercise at this moment. We’ll get
plenty of hands-on practice interacting with APIs in our upcoming lectures.

So, rest up and brace yourselves for the exciting coding challenges that lie ahead!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy