0

I am trying to send a http GET request using python requests library. Following is my code.

#!/usr/bin/python3
import requests
import json

URL = some-elkstack-url

datam = {'ayyo' : 'vammo'}
data_json = json.dumps(datam)
payload = {'json_payload': data_json}
header={'Content-Type': 'application/json' }
r = requests.get(url=URL, headers=header, data=datam)
a = r.json()
print('\nResponse: \n')
print(a)

I am getting this HTTP error back from the server.

{'error': {'root_cause': [{'type': 'json_parse_exception', 'reason': "Unrecognized token 'ayyo': was expecting ('true', 'false' or 'null')\n at 
[Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@74cef381; line: 1, column: 6]"}], 'type': 'json_parse_exception', 'reason': "Unrecognized token 'ayyo': was expecting ('true', 'false' or 'null')\n at 
[Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@74cef381; line: 1, column: 6]"}, 'status': 500}

When I do a curl from the command line, with the same json data I can get a proper response. What's going wrong in my code?

4
  • The response is telling you why the request could not be completed: 'reason': "Unrecognized token 'ayyo': was expecting ('true', 'false' or 'null') Commented Jul 24, 2018 at 5:47
  • Actually when I run curl with the same JSON, the server isn't complaining. So I thought I may have missed some grammar while forming the JSON object. Commented Jul 24, 2018 at 5:49
  • What happens when you supply any of the recognized (true, false, null) tokens as a param? I also don't think you need to dump the datam dict. You should be able to pass it along the way it is. Commented Jul 24, 2018 at 5:50
  • I tried sending both with and without dumping. In fact in the code above, I sent without dumping. I will try and check what happens when I send true, false or null, but if the server had no problem with the same JSON when sent with cURL, shouldn't it accept in this code too? Commented Jul 24, 2018 at 6:05

1 Answer 1

6

Instead of data, you want to use the json parameter, as follows:

datam = {'ayyo' : 'vammo'}
r = requests.get(URL,headers={'Content-Type': 'application/json' }, json=datam)
a = r.json()
# so on

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! It really was a lifesaver!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.