Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中返回500服务器错误的Curl post请求_Python_Windows_Curl_Druid - Fatal编程技术网

python中返回500服务器错误的Curl post请求

python中返回500服务器错误的Curl post请求,python,windows,curl,druid,Python,Windows,Curl,Druid,当我通过Windows的CMD运行下面的curl请求时,它工作得非常好 curl -XPOST -H"Content-Type:application/json" http://my_url:8082/druid/v2/sql/ -d "{\"query\":\"SELECT DISTINCT(event) FROM programs\"}" 我试图使用urllib.requests在Python3中复制相同的调用 import urllib.request values = {'query

当我通过Windows的CMD运行下面的curl请求时,它工作得非常好

curl -XPOST -H"Content-Type:application/json" http://my_url:8082/druid/v2/sql/ -d "{\"query\":\"SELECT DISTINCT(event) FROM programs\"}"
我试图使用urllib.requests在Python3中复制相同的调用

import urllib.request

values = {'query':'SELECT DISTINCT(event) FROM programs'}
url = 'http://my_url:8082/druid/v2/sql'

data = urllib.parse.urlencode(values).encode("utf-8")
req = urllib.request.Request(url, data)
req.add_header("Content-Type","application/json")
response = urllib.request.urlopen(req)
the_page = response.read()
但是,python版本返回一个服务器错误

response = urllib.request.urlopen(req)
File "C:\Python\Python35\lib\urllib\request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "C:\Python\Python35\lib\urllib\request.py", line 472, in open
response = meth(req, response)
File "C:\Python\Python35\lib\urllib\request.py", line 582, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python\Python35\lib\urllib\request.py", line 510, in error
return self._call_chain(*args)
File "C:\Python\Python35\lib\urllib\request.py", line 444, in _call_chain
result = func(*args)
File "C:\Python\Python35\lib\urllib\request.py", line 590, in 
http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500: Server Error

有人能告诉我我做错了什么吗?

您需要先将数据转换为json,您正在发送一个字典

import json

data = urllib.parse.urlencode(json.dumps(values)).encode("utf-8")
或者,使用:


您需要先将数据转换为json,然后发送一个字典

import json

data = urllib.parse.urlencode(json.dumps(values)).encode("utf-8")
或者,使用:


经过更多的实验,我用json.dumps替换了urllib.parse.urlencode,从而使代码正常工作。该守则的内容如下:

import urllib.request, json

values = {'query':'SELECT DISTINCT(event) FROM programs'}
url = 'http://my_url/druid/v2/sql'

data = json.dumps(values).encode("utf-8")
req = urllib.request.Request(url, data)
req.add_header("Content-Type","application/json")
response = urllib.request.urlopen(req)
the_page = response.read()

经过更多的实验,我用json.dumps替换了urllib.parse.urlencode,从而使代码正常工作。该守则的内容如下:

import urllib.request, json

values = {'query':'SELECT DISTINCT(event) FROM programs'}
url = 'http://my_url/druid/v2/sql'

data = json.dumps(values).encode("utf-8")
req = urllib.request.Request(url, data)
req.add_header("Content-Type","application/json")
response = urllib.request.urlopen(req)
the_page = response.read()

谢谢,你的第二种方法奏效了,但第一种没有。经过更多的实验,我发现urlencode部分是不需要的。我在下面发布了正确的版本。汉克斯,你的第二种方法有效,但第一种方法无效。经过更多的实验,我发现urlencode部分是不需要的。我已经在下面发布了正确的版本