SamBremner
SamBremner

Reputation: 823

Indexing to elasticsearch 6.1 with python script

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

Upvotes: 1

Views: 407

Answers (1)

match
match

Reputation: 11060

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

Upvotes: 1

Related Questions