Objective:
Take the starter Python script below- it interacts with a simple "Fav Quotes" API that Chad spun up. As written, the starter code sends a GET request to retrieve all current quotes, displaying them to the user.
YOUR TASK:
- Add code that uses the
requestslibrary to send aPOSTrequest with a new quote. - Check if the
POSTrequest is successful (status code201), and if so print"Quote added successfully!" - Otherwise, print
"Failed to add quote!"with the error message. - You can see the API in your browser by clicking here!
Use this to help figure out how you can POST to this API!
Base URL:
https://aux1-7db214f2-7074-4400-abe0-2a5559030ea9.live.alta3.com/quotes
Endpoints:
-
GET /quotes
- Description: Returns a JSON object containing all saved quotes.
- Response Format:
{ "quotes": [ {"name": "Author", "quote": "This is a sample quote."} ] } - Expected Status:
200 OK
-
POST /quotes
- Description: Adds a new quote to the API.
- Request Body (JSON):
{ "name": "Author's Name", "quote": "The quote text here." } - Response:
- Success: Returns
{"message": "Quote added successfully"}with status code201 Created. - Error: If a quote by the same name exists, returns
{"error": "A quote by this name already exists"}with status code409 Conflict.
- Success: Returns
import requests
from pprint import pprint
def main():
# API URL
url = "https://aux1-7db214f2-7074-4400-abe0-2a5559030ea9.live.alta3.com/quotes"
# Send GET request to retrieve all quotes
response = requests.get(url)
# Check if the GET request was successful
if response.status_code == 200:
quotes = response.json().get("quotes", [])
print("Current quotes in the API:")
pprint(quotes)
else:
print("Failed to retrieve quotes!")
# TODO: Add code to send a POST request with a new quote
if __name__ == "__main__":
main()Hints
- Use
requests.post()to send data to the API. - Set the JSON data in the
dataparameter ofrequests.post()with{"name": "Author", "quote": "New quote text"}. - Check
response.status_codeto confirm if thePOSTwas successful.
(A) Solution
import requests
from pprint import pprint
def main():
# API URL
url = "https://aux1-7db214f2-7074-4400-abe0-2a5559030ea9.live.alta3.com/quotes"
# Send GET request to retrieve all quotes
response = requests.get(url)
# Check if the GET request was successful
if response.status_code == 200:
quotes = response.json().get("quotes", [])
print("Current quotes in the API:")
pprint(quotes)
else:
print("Failed to retrieve quotes!")
# Gather user input for a new quote
name = input("\nWho said your quote?\n>")
quote = input("\nWhat is the quote?\n>")
# Construct POST data
post_data = {
"name": name,
"quote": quote
}
# Send POST request to add a new quote
post_response = requests.post(url, json=post_data)
# Check if the POST request was successful
if post_response.status_code == 201:
print("Quote added successfully!")
else:
error_message = post_response.json().get("error", "Unknown error occurred!")
print(f"Failed to add quote: {error_message}")
if __name__ == "__main__":
main()