1

I am trying to index to elasticsearch using a python 2.7 script as follows:

from __future__ import print_function
import urllib, urllib2

#FORMDATA is a json format string that has been taken from a file
ELASTIC_URL = 'http://1.2.3.9:9200/indexname/entry/
req = urllib2.Request(ELASTIC_URL)
req.add_header('contentType', 'application/x-www-form-urlencoded')

response = urllib2.urlopen(req, FORMDATA, timeout=4).read() 
print(response)

I keep getting the error HTTP Error 406: Not Acceptable: HTTPError

i have also tried formatting the data with urllib.quote(FORMDATA) and get the same error. The data is not a dictionary it is a string that when converted to json is multi dimensional.

I think this is something to do with the fact the req header needs to specify the contentType to be the correct format but i'm struggling to workout what this is. I managed to do this import on elasticsearch 5.x but now on 3.x it doesn't seem to be working.

Any ideas??

1 Answer 1

1

Almost all elasticsearch API calls use Content-Type: application/json in the headers - this should be what you need here.

Also be aware that if you are submitting data, this will need to be in the form of a POST (or a PUT if generating your own id), not a GET request: https://www.elastic.co/guide/en/elasticsearch/guide/current/index-doc.html

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

2 Comments

Thanks for your response. That turned out to be the content type needed and its now working.
Just a s a note the urllib2.urlopen() method sends a post request as default if you specify data to send.

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.