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: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"}

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: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"}

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

Solution

Sure, here is a Python program that uses the requests library to update the entry on the API server:

import requests
import json

# Define the API endpoint
url = "http://host1.open.uom.lk:8080"

# Define the headers for the API request
headers = {
    'Content-Type': 'application/json'
}

# Define the data to be updated
data = {
    "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"
}

# Convert the data to JSON format
data_json = json.dumps(data)

# Send a PUT request to the API endpoint
response = requests.put(url, headers=headers, data=data_json)

# Print the JSON response
print(response.json())

Please note that you need to replace "http://host1.open.uom.lk:8080" with the actual URL of the product that you want to update. Also, this is a basic example and does not include error handling. You might want to add try/except blocks to handle potential errors, such as a failed connection to the server or a server error.

This problem has been solved

Similar Questions

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

Which Python module is commonly used to make HTTP requests?Question 2Answera.OSb.Urllibc.Requestd.Requests

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)

1 . Pertanyaan :Pada objek HTTP Server, metode yang digunakan untuk listen terdapat port tertentu adalah?A. run()B. runPort()C. listen()D. runOnPort()E. listenPort()2 . Pertanyaan :Metode yang digunakan untuk mengubah objek JSON menjadi string adalah?A. JSON.objectify()B. JSON.stringify()C. JSON.decode()D. JSON.encode()E. JSON.parse()3 . Pertanyaan :Metode yang digunakan untuk memberikan respon pada bagian header adalah?A. responseHeader()B. responseHead()C. writeHead()D. postHead()E. writeHeader()4 . Pertanyaan :Metode yang digunakan untuk mengubah string menjadi objek JSON adalah?A. JSON.encode()B. JSON.decode()C. JSON.parse()D. JSON.objectify()E. JSON.stringify()5 . Pertanyaan :Kode yang digunakan untuk mendapatkan path API yang diakses adalah?A. url.parse(req.url, true).pathname;;B. url.get(req.url, true).path;C. url.parse(req.url, true).pathname;D. url.get(req.url, true).path;E. url.get(req.url, true).basePath;6 . Pertanyaan :Pada objek HTTP Server, metode yang digunakan untuk menutup koneksi server adalah?A. cut()B. close()C. terminate()D. end()E. timeout()7 . Pertanyaan :Metode yang digunakan untuk membuat server adalah?A. addServer()B. listenServer()C. mockServer()D. getServer()E. createServer()8 . Pertanyaan :Kode yang digunakan untuk mendapatkan parameter pada URL setelah simbol ? adalah?A. url.get(req.url, true).urlParam;B. url.get(req.url, true). parameter;C. url.parse(req.url, true).query;D. url.get(req.url, true).query;E. url.parse(req.url, true).parameter;9 . Pertanyaan :Metode yang digunakan untuk mengatur content-type adalah?A. putContentType()B. setHeader()C. putHeaderD. createContentType()E. setContentType()10 . Pertanyaan :Metode yang digunakan untuk memberikan respon adalah?A. res.writeFile()B. res.response()C. res.send()D. res.sendFile()E. res.end()

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.