Fakhr Ali
Fakhr Ali

Reputation: 128

Data is not being POSTed in python. No errors given by compiler

I'm trying to POST data to an api, after execution the compiler does not give any errors, but the data is not showing in the database.

import requests
endpoint = "http://192.168.10.2:8085/api/customer"
myObj = {"customer_id": 900, 
    "customer_code": "qwertyuiop", 
    "ustomer_name": "lion", 
    "contact": "030190000", 
    "address": "lane"}
x = requests.post(url = endpoint, data = myObj)

Edit: when I try to do

print(x.text) 

I get this error:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"00-9b47b6c7ce1f14499652ba95b3faca3a-cc8e04f53a002647-00"}

Any help would be appreciated.

Upvotes: 0

Views: 53

Answers (1)

Fakhr Ali
Fakhr Ali

Reputation: 128

I found the problem, I needed to use json (which takes a dictionary) instead of data (which takes a string).

import json
import requests

payload = {"customer_id": 456, "customer_code": "fakhr", "customer_name": "fakhr", "contact": "fakhr", "address": "fakhr"}
r = requests.post("http://192.168.10.2:8085/api/customer", json=payload)
print(r.text)

Upvotes: 1

Related Questions