Knowee
Questions
Features
Study Tools

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:8080Write 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"        },......}

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:8080Write 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"        },......}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure, here is a Python program that uses the requests library to retrieve all the products from the API server and print the total number of products currently stored in the server.

import requests
import json

# Define the base URL
base_url = "http://host1.open.uom.lk:8080"

# Send a GET request to the server
response = requests.get(base_url)

# Convert the response to JSON
data = response.json()

# Check if the request was successful
if data['message'] == 'success':
    # Print the total number of products
    print("Total number of products: ", len(data['data']))
else:
    print("Failed to retrieve data")

This program first sends a GET request to the server. It then converts the server's response to JSON format. If the 'message' field in the response is 'success', it prints the total number of products by getting the length of the 'data' field. If the 'message' field is not 'success', it prints an error message.

This problem has been solved

Solution 2

Sure, here is a Python program that uses the requests library to retrieve all the products from the API server and print the total number of products currently stored in the server.

import requests
import json

# Define the base URL
base_url = "http://host1.open.uom.lk:8080"

# Send a GET request to the server
response = requests.get(base_url)

# Convert the response to JSON
data = response.json()

# Get the list of products from the data
products = data["data"]

# Print the total number of products
print("Total number of products: ", len(products))

This program works by sending a GET request to the server, which should return a JSON response containing all the products. The json() method is used to convert this response into a Python dictionary, and the "data" key is used to access the list of products. The len() function is then used to count the number of products in this list.

This problem has been solved

Similar Questions

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"        },......}Answer:(penalty regime: 0 %)12345678910111213141516171819202122232425import requestsimport json# Define the URLurl = "http://host1.open.uom.lk:8080"# Define the headersheaders = {'Content-Type': 'application/json'}# Define the product datadata = { "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"}# Convert the data to JSON formatjson_data = json.dumps(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:8080Write a python program to create a new product with the following information in JSON format. Print the response code of the request.{        "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"}Answer:(penalty regime: 0 %)

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:8080The following product has been entered into the API server by you in the previous question.However, it has been noted that the entered product's brand should be changed to "Araliya" instead of "CIC".Write a python program to update the entry on the API server as required. Print the JSON response of the request.{        "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"}

0. Gather data from an API

How will you read data from an API? Briefly mention the steps involved.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.