我正在尝试将数据POST到api,执行后编译器不会给出任何错误,但数据不会显示在数据库中。
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)
编辑:当我尝试做时
print(x.text)
我得到这个错误:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"00-9b47b6c7ce1f14499652ba95b3faca3a-cc8e04f53a002647-00"}
如有任何帮助,我们将不胜感激。
我发现了问题,我需要使用json(需要字典(而不是数据(需要字符串(。
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)