我可以使用Advanced Rest Client,但不能使用python请求。这就是它应该看起来的样子:
POST https://somedomain.com/dev/WebAPI/api/comment HTTP/1.1
Authorization: Token YOUR_TOKEN
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryPnWcjZhRHdKiAWdv
------WebKitFormBoundaryPnWcjZhRHdKiAWdv
Content-Disposition: form-data; name="rootMessageId"
14697
------WebKitFormBoundaryPnWcjZhRHdKiAWdv
Content-Disposition: form-data; name="file"; filename="mypicture.jpg"
Content-Type: image/jpeg
<image data>
------WebKitFormBoundaryPnWcjZhRHdKiAWdv--
我的要求如下:
headers = {"Authorization": "Token mysecrettoken", "Content-Type":"application/x-www-form-urlencoded"}
files = {'file': open("861311006159023.jpg", "rb")}
payload = {"rootMessageId":284737}
r = requests.post("https://somedomain.com/za/WebAPI/api/comment", headers=headers, data=payload, files=files)
print(r.json())
此回归
rootMessageId属性是必需的,不能为NULL或0。
我正在使用application / x-www-form-urlencoded只是因为Advanced Rest Client可以做到这一点并且有效。当我从请求标题中删除内容类型时,似乎图像数据不正确。
当我将内容类型更改为mulitiform / form-data时,它会抛出此错误:
> obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
> raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
> 0)