3

I cannot wrap my brain around this issue:

When I run this code in my IDE (pycharm), or via the command line, I get a 204 HTTP response and no content. When I set breakpoints in my debugger to see what is happening, the code executes fine and r.content and r.text are populated with the results from the request. r.status_code also has a value of 200 when running in the debugger.

code:

    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)

output when run through debugger:

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

When I execute the code normally without the debugger, i get a 204 on the second response.

output:

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

I am guessing this has something to do with the debugger slowing down the requests and allowing the server to respond with the data? This seems like a race condition. I've never run into this with requests.

Is there something I am doing wrong? I'm at a loss. Thanks in advance for looking.

dobbs
  • 1,089
  • 6
  • 22
  • 45
  • If that is the case test it using a time.sleep(10). You could also check the verbose debug out put described in this post to see if there is a difference. http://stackoverflow.com/questions/10588644/how-can-i-see-the-entire-http-request-thats-being-sent-by-my-python-application?rq=1 – NotARobot Mar 13 '17 at 22:15

3 Answers3

0

Solved by adding this loop:

    while r.status_code == 204:
        time.sleep(1)
        r = requests.get(self.resulturl, verify=False, auth=HTTPBasicAuth(self.user, self.passwd))

As I suspected the Rest API was taking longer to collect results, hence the 204. When running the debugger, it slowed the process long enough that the API was able to complete the initial request, thus giving a 200.

dobbs
  • 1,089
  • 6
  • 22
  • 45
0

The HTTP 204 No Content success status response code indicates that the request has succeeded, but that the client doesn't need to go away from its current page. A 204 response is cacheable by default. Below settings would solve the issue.

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)

The 204 response is converted to 200. Please check below logs.

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]>

Thanks

toshiro92
  • 1,287
  • 5
  • 28
  • 42
0

In this case , after an SID generation , directly trying to get results when the status response is 200 but the dispatchState is not DONE. So when checking results response it gives 204.

we can keep checking the status of job ("<s:key name="dispatchState">DONE</s:key>" ) by filtering.So once the dispatchState shows DONE , go for checking results , then response code would directly give 200.