# Elasticsearch query using API manager

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-using-api-manager/108941>\
**Category:** Elasticsearch\
**Created:** [November 23, 2017, 9:56pm UTC](https://discuss.elastic.co/t/elasticsearch-query-using-api-manager/108941 "2017-11-23T21:56:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowy](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@snowy](https://discuss.elastic.co/u/snowy)\
**Post date:** [November 23, 2017, 9:56pm UTC](https://discuss.elastic.co/t/elasticsearch-query-using-api-manager/108941/1 "2017-11-23T21:56:28Z")

</div>

I am involved in a project for work where I have to query an Elasticsearch server using an API manager and Python. I have the below code but the error I'm getting indicates I'm using the wrong content-type. I'm not sure which one I should be using though.

> import requests  
> import elasticsearch  
> from elasticsearch import Elasticsearch
> 
> headers = {  
> 'apikey': 'myapikey',  
> 'Content-Type': 'application/json'  
> }
> 
> data = {'query': '{"bool": { "must": [{ "match": { "message": "[gmail.com](http://gmail.com) [yahoo.com](http://yahoo.com)" } },{ "range" : { "@timestamp": { "gte" : "now-7d"} } } ]}}}'}
> 
> r = requests.get('[https://myapiserver/pastebin-\*/\_search](https://myapiserver/pastebin-*/_search)', headers=headers, data=data)  
> print r.status\_code  
> print r.text

The Error:

> {"error":{"root\_cause":[{"type":"json\_parse\_exception","reason":"Unrecognized token 'query': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@261eb2fe; line: 1, column: 7]"}],"type":"json\_parse\_exception","reason":"Unrecognized token 'query': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@261eb2fe; line: 1, column: 7]"},"status":500}

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [November 29, 2017, 3:47am UTC](https://discuss.elastic.co/t/elasticsearch-query-using-api-manager/108941/2 "2017-11-29T03:47:35Z")

</div>

It's because you are using a generic http client library so `data` must be a full JSON string and you are using a slightly different syntax there. Try using the following instead:

> data = '{"query": {"bool": { "must": [{ "match": { "message": "[gmail.com](http://gmail.com) [yahoo.com](http://yahoo.com)" } },{ "range" : { "@timestamp": { "gte" : "now-7d"} } } ]}}}'

But you have imported the standard Elasticsearch client there, why not use that instead of `requests`?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 27, 2017, 3:48am UTC](https://discuss.elastic.co/t/elasticsearch-query-using-api-manager/108941/3 "2017-12-27T03:48:12Z")

</div>

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