我无法解决这个问题:
当我在IDE(pycharm)中运行此代码或通过命令行运行此代码时,我得到204
HTTP响应而没有内容。当我在调试器中设置断点以查看发生的情况时,代码执行正常,r.content
和r.text
将填充请求的结果。在调试器中运行时,r.status_code
的值也为200
。
代码:
r = requests.post(self.dispatchurl, verify=False, auth=HTTPBasicAuth(self.user, self.passwd))
print 'first request to get sid: status {}'.format(r.status_code)
json_data = json.loads(r.text)
self.sid = json_data['sid']
print 'the sid is: {}'.format(self.sid)
self.getresulturl = '{}/services/search/jobs/{}/results{}'.format(self.url, self.sid, self.outputmode)
x = requests.get(self.getresulturl, verify=False, auth=HTTPBasicAuth(self.user, self.passwd))
print 'second request to get the data: status {}'.format(x.status_code)
print 'content: {}'.format(x.text)
运行调试器时输出:
first request to get sid: status 201
the sid is: sanitizedatahere
second request to get the data: status 200
content: {"preview":false...}
Process finished with exit code 0
当我在没有调试器的情况下正常执行代码时,我会在第二个响应中得到204
。
输出:
first request to get sid: status 201
the sid is: sanitizedatahere
second request to get the data: status 204
content:
Process finished with exit code 0
我猜这与调试器有关,这会减慢请求并允许服务器响应数据?这似乎是竞争条件。我requests
从未遇到此问题。
我做错了吗?我不知所措。提前感谢您的光临。
答案 0 :(得分:0)
通过添加此循环解决:
while r.status_code == 204:
time.sleep(1)
r = requests.get(self.resulturl, verify=False, auth=HTTPBasicAuth(self.user, self.passwd))
我怀疑Rest API需要更长时间来收集结果,因此204
。运行调试器时,它会使进程运行得足够长,以至于API能够完成初始请求,从而得到200
。
答案 1 :(得分:0)
HTTP 204 No Content成功状态响应代码表示请求已成功,但是客户端不需要离开当前页面。默认情况下,204响应是可缓存的。 进行以下设置可以解决该问题。
r = requests.get(splunk_end, headers=headers, verify=False)
while r.status_code == 204:
time.sleep(1)
r = requests.get(splunk_end, headers=headers, verify=False)
204响应转换为200。请检查以下日志。
https://localhost:8089/services/search/jobs/4D44-A45E-7BDB8F0BE473/results?output_mode=json
/usr/lib/python2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
/usr/lib/python2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
<Response [204]>
/usr/lib/python2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
<Response [200]>
谢谢
答案 2 :(得分:0)
在这种情况下,在生成SID之后,如果状态响应为200但dispatchState不是DONE,则直接尝试获取结果。因此,当检查结果响应时,它的值为204。
我们可以通过过滤来继续检查作业的状态(“ DONE ”)。因此,一旦dispatchState显示DONE,请检查结果,然后返回响应代码会直接给200。