How Write a python program to retrieve all the products from the API Server
Question:
Suppose you are writing a python web client to access an API of an online supermarket. Given below are the API details.
Base URL = http://host1.open.uom.lk:8080
Write a python program to retrieve all the products from the API Server and print the total number of products currently stored in the server.
Hint: the json response will be of the following example format:
{
"message": "success",
"data": [
{
"id": 85,
"productName": "Araliya Basmathi Rice",
"description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.",
"category": "Rice",
"brand": "CIC",
"expiredDate": "2023.05.04",
"manufacturedDate": "2022.02.20",
"batchNumber": 324567,
"unitPrice": 1020,
"quantity": 200,
"createdDate": "2022.02.24"
},
{
"id": 86,
"productName": "Araliya Basmathi Rice",
"description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.",
"category": "Rice",
"brand": "CIC",
"expiredDate": "2023.05.04",
"manufacturedDate": "2022.02.20",
"batchNumber": 324567,
"unitPrice": 1020,
"quantity": 200,
"createdDate": "2022.02.24"
}
]
}
The Answer For The Above Question Is The Code Below
Thank You.
import requests
import json
BASE_URL = "http://host1.open.uom.lk:8080"
updated_entity = {
"productName":"Araliya Basmathi Rice",
"description":"White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.",
"category":"Rice",
"brand":"Araliya",
"expiredDate":"2023.05.04",
"manufacturedDate":"2022.02.20",
"batchNumber":324567,
"unitPrice":1020,
"quantity":200,
"createdDate":"2022.02.24"
}
response = requests.put(f"{BASE_URL}/api/products/", json=updated_entity)
print(response.json())
Answers:
Great! So it sounds like you were able to fetch the JSON response successfully.
Mind you, response_API
is NOT the JSON, it is just a Response object. You need to call .json() or .text() on it to get the result you intend. Check this out: https://www.w3schools.com/python/ref_requests_response.asp
You’ll now need to parse the JSON string into a useable Python data structure.
Here is information for parsing JSON: https://www.w3schools.com/python/gloss_python_json_parse.asp
parsed = json.load(response_API.text())
JSON is parsed into a dictionary. Here you can access the “data” key, which is a list of products. We can simply get the length of a list.
len(parsed[“data”])
Suppose you are writing a python web client to access an API of an online supermarket. Given below are the API details.
Base URL = http://host1.open.uom.lk:8080
Write a python program to retrieve all the products from the API Server and print the total number of products currently stored in the server.
Hint: the json response will be of the following example format:
{
"message": "success",
"data": [
{
"id": 85,
"productName": "Araliya Basmathi Rice",
"description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.",
"category": "Rice",
"brand": "CIC",
"expiredDate": "2023.05.04",
"manufacturedDate": "2022.02.20",
"batchNumber": 324567,
"unitPrice": 1020,
"quantity": 200,
"createdDate": "2022.02.24"
},
{
"id": 86,
"productName": "Araliya Basmathi Rice",
"description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.",
"category": "Rice",
"brand": "CIC",
"expiredDate": "2023.05.04",
"manufacturedDate": "2022.02.20",
"batchNumber": 324567,
"unitPrice": 1020,
"quantity": 200,
"createdDate": "2022.02.24"
}
]
}
The Answer For The Above Question Is The Code Below
Thank You.
import requests
import json
BASE_URL = "http://host1.open.uom.lk:8080"
updated_entity = {
"productName":"Araliya Basmathi Rice",
"description":"White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.",
"category":"Rice",
"brand":"Araliya",
"expiredDate":"2023.05.04",
"manufacturedDate":"2022.02.20",
"batchNumber":324567,
"unitPrice":1020,
"quantity":200,
"createdDate":"2022.02.24"
}
response = requests.put(f"{BASE_URL}/api/products/", json=updated_entity)
print(response.json())
Great! So it sounds like you were able to fetch the JSON response successfully.
Mind you, response_API
is NOT the JSON, it is just a Response object. You need to call .json() or .text() on it to get the result you intend. Check this out: https://www.w3schools.com/python/ref_requests_response.asp
You’ll now need to parse the JSON string into a useable Python data structure.
Here is information for parsing JSON: https://www.w3schools.com/python/gloss_python_json_parse.asp
parsed = json.load(response_API.text())
JSON is parsed into a dictionary. Here you can access the “data” key, which is a list of products. We can simply get the length of a list.
len(parsed[“data”])