[英]POST request list python-requests
I am trying to POST Boxes = [{'X': 2423, 'Y': 532, 'W': 7456, 'H': 2345}]
to the DRF server, I receive status code 200, However, on the server, I receive weird format: "Boxes": "[{\"X\": 1028, \"Y\": 228, \"W\": 711, \"H\": 852}]"
.我正在尝试POST
Boxes = [{'X': 2423, 'Y': 532, 'W': 7456, 'H': 2345}]
到 DRF 服务器,我收到状态代码 200,但是,在服务器上,我收到奇怪的格式: "Boxes": "[{\"X\": 1028, \"Y\": 228, \"W\": 711, \"H\": 852}]"
。
I don't understand where the '/' comes from.我不明白'/'来自哪里。
Here is the POST request in python:这是 python 中的POST请求:
Boxes = [{"X": 2423, "Y": 532, "W": 7456, "H": 2345}]
url = "http://art1x.pythonanywhere.com/snippets/1/"
username = "artemii"
password = "admin"
data = {"Boxes": json.dumps(Boxes)}
response = requests.put(url, auth=(username, password), data=data)
print(response.status_code)
'Boxes' is a list “盒子”是一个列表
Please help!请帮忙! Any suggestion is valuable
任何建议都很有价值
The weird format looks like escape characters.奇怪的格式看起来像转义字符。 Setting content type should fix this i think.
我认为设置内容类型应该可以解决这个问题。
eg:例如:
headers["Content-Type"]="application/x-www-form-urlencoded"
Remove json.dumps
if you want to send Boxes as json object instead of a string:如果您想将 Box 发送为 json object 而不是字符串,请删除
json.dumps
:
data = {"Boxes": Boxes}
json.dumps(Boxes)
convert Boxes into a string. json.dumps(Boxes)
将 Boxes 转换为字符串。 requests.put
then convert data into string for transport, where it inserts escape characters because the content is a string. requests.put
然后将数据转换为字符串以进行传输,其中插入转义字符,因为内容是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.