放置请求在curl中工作,但在Python中不工作 [英] Put request working in curl, but not in Python

查看:57
本文介绍了放置请求在curl中工作,但在Python中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用curl创建一个'put'方法,一切正常,并且我得到了JSON:

I am trying to make a 'put' method with curl everything is working fine and I got the JSON back:

curl -X PUT -d '[{"foo":"more_foo"}]' http://ip:6001/whatever?api_key=whatever

但是由于某些原因,如下所述使用python requests 模块:

But for some reason when using the python requests module as follow:

import requests
url = 'http://ip:6001/whatever?api_key=whatever'

a = requests.put(url, data={"foo":"more_foo"})

print(a.text)
print(a.status_code)

我收到以下错误:

500 Internal Server Error

内部服务器错误

服务器遇到内部错误,无法完成您的请求.服务器超载或应用程序中有错误.

500 Internal Server Error

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

NB :服务器已启动并正在运行.

NB: The server is up and running.

推荐答案

数据应转储:

a = requests.put(url, data=json.dumps([{"foo":"more_foo"}]))

,或者您可以使用 json 键代替 data :

or you can use the json key instead of data:

a = requests.post(url, json=[{"foo":"more_foo"}])

这篇关于放置请求在curl中工作,但在Python中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆