1

Query using curl.

curl -XGET http://localhost:9200/users/_search?pretty=true -H 'Content-Type: application/json' -d '
  { 
      "from" : 0, "size" : 5,
      "query" : {
          "query_string" : {
              "query" : "Port"
          }
      }
  }
'

Same thing i am trying to do in python requests:

url = f'{URL}/{INDEX}/_search/'
es_query = { 
    "query" : {
        "query_string" : {
            "query" : "Port"
        }
    }
}
res = requests.get(url, data=es_query) 
print(res.json())

I am getting below error while doing this.

`{'error': 'Content-Type header [application/x-www-form-urlencoded] is not supported', 'status': 406}`

Please take a look what can be the issue

2 Answers 2

1

You can use curlconverter.com to easily convert cURL commands to Python requests. Here's the output for your cURL command:

import requests

params = {
    'pretty': 'true',
}

json_data = {
    'from': 0,
    'size': 5,
    'query': {
        'query_string': {
            'query': 'Port',
        },
    },
}

response = requests.get('http://localhost:9200/users/_search', params=params, headers=headers, json=json_data)
Sign up to request clarification or add additional context in comments.

Comments

0

Your URL is wrong

url = f'{URL}/{INDEX}/_bulk/'

Should be

url = f'{URL}/{INDEX}/_search/'

Also you need to specify the following request header

Content-type: application/json

Like this:

res = requests.get(url, json=es_query, headers={"Content-type": "application/json"}) 

2 Comments

{'error': {'root_cause': [{'type': 'json_parse_exception', 'reason': "Unrecognized token 'query': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 1, column: 7]"}], 'type': 'json_parse_exception', 'reason': "Unrecognized token 'query': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 1, column: 7]"}, 'status': 400}
Not getting this error.

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.