如何将PUT请求发送到API而不出现415错误

时间:2020-05-07 09:16:17

标签: python json http

我正在尝试使用Python脚本将PUT请求发送到API,但始终收到415错误。 有人可以指出我的错误吗? 这是我的代码:

for item in data:
    print(type(item["Status"]))
    print(type(item["Text"]))
    r = requests.put("http://127.0.0.1:8000/api/ratings/" + str(item["id"]) , {"Text": item["Text"], "Status": item["Status"]})
    print(r)

这是415条消息:

<class 'numpy.str_'>
<class 'str'>
<Response [415]>
<class 'numpy.str_'>
<class 'str'>
<Response [415]>
<class 'numpy.str_'>
<class 'str'>
<Response [415]>
<class 'numpy.str_'>
<class 'str'>
<Response [415]>

服务器错误:

May  7 16:31:30 |DEBUG| May  7 16:31:30 |P3mWARN W |ERSPUT  (415)  api/ratings/4.json/hosth"127.0.0.1:8004" =pi"127.0.0.1" =0.0.1" .0.1" .msch0.1" ch0.1" s0.1" .1" s3936mschemes"https"36mschemehemettps"36mscheme=s"36mschemehememeay  7 16:31:30 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\AddFormatListener::onKernelRequest". path="C:\\xampp\\php\\php-cgi.exe" php="7.4.4"

1 个答案:

答案 0 :(得分:0)

也许您想要做的是使用键data作为参数来进行传递对象作为参数的请求。

还要添加application/json的标题。 试试这个:

headers = {'Content-type':'application/json', 'Accept':'application/json'}
r = request.put("http://127.0.0.1:8000/api/ratings/" + str(item["id"]),
                                                       data=item,
                                                       headers=headers)