Search code examples
pythonpython-requestswebapiweb-api-testing

How to enter a new entry to a API server using requests.post?


This is the question from the course I am following

I am getting an error while trying to put the required details by using requests.post. When I am using request.post the wesite should be uploaded with the details i am posting right? But it does not show anything that i am uploading.

import requests
BASE_URL = 'http://host1.open.uom.lk:8080'
product_entity = {"products":{"product": {"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.02.04", "manufacturedDate": "2022.002.20", "batchNumber": "324567", "unitPrice": "1020", "quantity": "200", "createdDate": "2022.02.24"  }}}
response = requests.post(f"{BASE_URL}/products", json=product_entity)
print(response.status_code)
#print(response.json())

This is my code. But it does not result the details being uploaded to the website. Can anyone help me with understanding the error please?

This is the error message I am getting


Solution

  • The status code respond that I was getting was 404. Which is the endpoint not found. In the question, the endpoint that should be used is given as /api/products/ for requests.post .

    This is the modified code.

    import requests
    BASE_URL = 'http://host1.open.uom.lk:8080'
    product_entity = {"products":{"product": {"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.02.04", "manufacturedDate": "2022.002.20", "batchNumber": "324567", "unitPrice": "1020", "quantity": "200", "createdDate": "2022.02.24"  }}}
    response = requests.post(f"{BASE_URL}/api/products", json=product_entity)
    print(response.status_code)
    #print(response.json())