I want to use the Python 3 module urllib to access an Elasticsearch database at localhost:9200
. My script gets a valid request (generated by Kibana) piped to STDIN in JSON format.
Here is what I did:
import json
import sys
import urllib.parse
import urllib.request
er = json.load(sys.stdin)
data = urllib.parse.urlencode(er)
data = data.encode('ascii')
uri = urllib.request.Request('http://localhost:9200/_search', data)
with urllib.request.urlopen(uri) as repsonse:
response.read()
(I understand that my repsonse.read()
doesn't make much sense by itself but I just wanted to keep it simple.)
When I execute the script, I get an
HTTP Error 400: Bad request
I am very sure that the JSON data I'm piping to the script is correct, since I had it printed and fed it via curl
to Elasticsearch, and got back the documents I expected to get back.
Any ideas where I went wrong? Am I using urllib
correctly? Do I maybe mess up the JSON data in the urlencode
line? Am I querying Elasticsearch correctly?
Thanks for your help.