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 %)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)
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 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 %)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)
Solution
Send the POST request
response = requests.post(url, headers=headers, data=json_data)
Print the response code
print(response.status_code)
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 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" },......}
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"}
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)
Which Python module is commonly used to make HTTP requests?Question 2Answera.OSb.Urllibc.Requestd.Requests
import randomimport jsondef intermediate_step_multi_run_process(request): def generate(): counter = 0 while counter < 2: time.sleep(0.3) random_no_list = [] for i in range(1000000000): random_no = random.random() ** 2 random_no_list.append(random_no) random_no_list = json.dumps(random_no_list) yield "data:" + random_no_list + "\n\n" counter += 1 return StreamingHttpResponse(generate(), content_type="text/event-stream") The above stated is an async function triggered by a websocket in a web application to render the list of 100 million squared random numbers. What is the possible error that this piece of code could result in a wsgi application and why?
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.