POST SQL query using Python requests - not working

Hi,

I'm trying run below SQL query which works in Kibana console:

POST /_sql?format=csv
{
  "query": "DESCRIBE \"as-log-metrics-2019.06.14\" " 
}

using Python and requests module:

json_data = {"query": "DESCRIBE \"as-log-metrics-2019.06.14\" "}
r = requests.post("http://localhost:9200/_sql?format=csv", data=json_data, headers={'Content-Type':'application/json'})
print(r)
print(r.text)
print(r.request.headers)
print(r.request.body)

but it's doesn't work.

<Response [400]>
{"error":{"root_cause":[{"type":"x_content_parse_exception","reason":"[1:1] [sql/query] failed to parse object"}],"type":"x_content_parse_exception","reason":"[1:1] [sql/query] failed to parse object","caused_by":{"type":"json_parse_exception","reason":"Unrecognized token 'query': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@63ec79a8; line: 1, column: 7]"}},"status":400}
{'Accept': '*/*', 'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Content-Type': 'application/json', 'Content-Length': '47', 'User-Agent': 'python-requests/2.19.1'}
query=DESCRIBE+%22as-log-metrics-2019.06.14%22+

What am i doing wrong ?

Thanks in advance !

Ged

Got it resolved if anyone will face the same problem.

json_data = json.dumps({"query": "DESCRIBE \"as-log-metrics-2019.06.14\" "})
r = requests.post("http://localhost:9200/_sql?format=csv", data=json_data, headers={'Content-Type':'application/json'})
print(r.text)
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.