# Elasticsearch api returning empty response (python)

**URL:** <https://discuss.elastic.co/t/elasticsearch-api-returning-empty-response-python/344238>\
**Category:** Elasticsearch\
**Created:** [October 2, 2023, 3:22pm UTC](https://discuss.elastic.co/t/elasticsearch-api-returning-empty-response-python/344238 "2023-10-02T15:22:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aidan\_Campbell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aidan_campbell/32/125623_2.png) [@Aidan\_Campbell](https://discuss.elastic.co/u/Aidan_Campbell)\
**Post date:** [October 2, 2023, 3:22pm UTC](https://discuss.elastic.co/t/elasticsearch-api-returning-empty-response-python/344238/1 "2023-10-02T15:22:48Z")

</div>

I am trying to retrieve elasticsearch data in python using the elasticsearch rest api.  
When I attempt to call the search api using the requests python library, the elasticsearch python client, or directly from the command line, I get an empty response that looks like this:  
`{'took': 0, 'timed_out': False, '_shards': {'total': 0, 'successful': 0, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': 0.0, 'hits': []}}`

This response has a code of 200 and no errors occur. When I tried running the same request in the elasticsearch API console, however, I get the correct, non-empty response.  
Can anyone shed some light as to why I can get data from the api console but not by calling the api from python or the command line?  
As far as I can tell, the syntax is correct, I have the right permissions, and I double checked the auth codes and endpoint url.

Python code (request library)

```auto
HEADERS = {
      'Content-Type': 'application/json',
      'kbn-xsrf': 'reporting',
      'Authorization': 'ApiKey <my_APIKEY>'
  }

url = "https://<my_ENDPOINT_URL>:9243/logs-*/_search"
r = requests.get(url, headers=HEADERS)
print(r)

```

Python code (Elasticsearch client):

```auto
client = Elasticsearch(
      cloud_id=<my_CLOUD_ID>,
      api_key=("<my_api-key-id>", "<my_api-key-secret>")
  )

r = client.search(index="logs-*", query={"match_all": {}})
print(r)

```

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [October 2, 2023, 3:26pm UTC](https://discuss.elastic.co/t/elasticsearch-api-returning-empty-response-python/344238/2 "2023-10-02T15:26:18Z")

</div>

> [@Aidan\_Campbell](#):
>
> `index="logs*-"`

This seems to be wrong, it will look for indices that start with `logs` and end with an hyphen, `-`.

Is this how it is in your code or it is just a typo?

It should be `logs-*`.

---

<div class="post-metadata">

**Author:** ![Aidan\_Campbell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aidan_campbell/32/125623_2.png) [@Aidan\_Campbell](https://discuss.elastic.co/u/Aidan_Campbell)\
**Post date:** [October 2, 2023, 3:29pm UTC](https://discuss.elastic.co/t/elasticsearch-api-returning-empty-response-python/344238/3 "2023-10-02T15:29:27Z")

</div>

That was just a typo in my post. The post has been updated.

---

<div class="post-metadata">

**Author:** ![Aidan\_Campbell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aidan_campbell/32/125623_2.png) [@Aidan\_Campbell](https://discuss.elastic.co/u/Aidan_Campbell)\
**Post date:** [October 12, 2023, 3:10pm UTC](https://discuss.elastic.co/t/elasticsearch-api-returning-empty-response-python/344238/4 "2023-10-12T15:10:46Z")

</div>

Fixed the issue. role\_descriptors/indices/names needed to be set to "\*" in the API Key I was using. The role\_descriptors/indices/privileges also needed to include "read" and "view\_index\_metadata". (see: [Create API key API | Elasticsearch Guide [8.10] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html#security-api-create-api-key-request))

---

<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:** [November 9, 2023, 3:11pm UTC](https://discuss.elastic.co/t/elasticsearch-api-returning-empty-response-python/344238/5 "2023-11-09T15:11:40Z")

</div>

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