# Get search with json request

**URL:** <https://discuss.elastic.co/t/get-search-with-json-request/287616>\
**Category:** Elasticsearch\
**Created:** [October 25, 2021, 6:06pm UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616 "2021-10-25T18:06:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![razs](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@razs](https://discuss.elastic.co/u/razs)\
**Post date:** [October 25, 2021, 6:06pm UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/1 "2021-10-25T18:06:07Z")

</div>

Hi, how can i get a search with json and python?  
0

Been reading this [Run Elasticsearch API requests | Kibana Guide [7.15] | Elastic](https://www.elastic.co/guide/en/kibana/current//console-kibana.html) and im having doubst, can anyone help me create a json request to run from python to do this?

this is the link that i whant to convert to json request:  
[https://log-viewer.dev/app/logtrail#/q="var1"%20AND%20"var2"&h=All&t=Now&i=filebeat-\*&\_g=()](https://log-viewer.dev/app/logtrail#/q=%22var1%22%20AND%20%22var2%22&h=All&t=Now&i=filebeat-*&_g=())

Been playing with this but i get the html of the page not the response

```auto
import requests

url = "https://log-viewer.dev/app/logtrail#/?i=filebeat-*&_g=()&q="

payload={}
headers = {
        'text': 'flow',
        'date': '2021-10-25'
}

response = requests.request("GET", url, headers=headers, data=payload, timeout=55)

print(response.text)

```

Im getting

```auto
> {
> "statusCode": 404,
> "error": "Not Found",
> "message": "Not Found"
> }

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 25, 2021, 9:58pm UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/2 "2021-10-25T21:58:00Z")

</div>

Welcome to our community! 😃

It's not clear why you are querying logtrail here, and not Elasticsearch directly. Can you elaborate why you are taking this approach?

---

<div class="post-metadata">

**Author:** ![razs](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@razs](https://discuss.elastic.co/u/razs)\
**Post date:** [October 26, 2021, 7:55am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/3 "2021-10-26T07:55:00Z")

</div>

its the first time im doing this, so maybe im not doing it the right way.

I have a python program and i need to get the result from the search above, i thought ib doing it thru a json request, do you advice doing this in a diferent way?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 26, 2021, 8:00am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/4 "2021-10-26T08:00:28Z")

</div>

I don't know logtrail but does it have an API? If not, then you will need to query Elasticsearch directly.

---

<div class="post-metadata">

**Author:** ![razs](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@razs](https://discuss.elastic.co/u/razs)\
**Post date:** [October 26, 2021, 8:06am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/5 "2021-10-26T08:06:54Z")

</div>

yes, it can be done, i have a link in my post, just having dificultys setting it up, thats why i decide to ask for some help.

Am i in the wrong section?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 26, 2021, 8:10am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/6 "2021-10-26T08:10:33Z")

</div>

I am not sure what sort of logtrail experience people here have, so it's unlikely we will be unable to help with that aspect. You will need to ask the logtrail devs/community if you really want to go that route.

However, you can definitely request Elasticsearch directly with Python. eg from [Python Elasticsearch Client — Elasticsearch 7.15.1 documentation](https://elasticsearch-py.readthedocs.io/en/v7.15.1/) and making a super basic example;

```auto
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()

res = es.search(index="filebeat-*", query={"match_all": {}})
print("Got %d Hits:" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
    print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

```

---

<div class="post-metadata">

**Author:** ![razs](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@razs](https://discuss.elastic.co/u/razs)\
**Post date:** [October 26, 2021, 8:16am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/7 "2021-10-26T08:16:52Z")

</div>

> [@warkolm](#):
>
> ```auto
> from datetime import datetime
> from elasticsearch import Elasticsearch
> es = Elasticsearch()
> 
> res = es.search(index="filebeat-*", query={"match_all": {}})
> print("Got %d Hits:" % res['hits']['total']['value'])
> for hit in res['hits']['hits']:
> print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
> 
> ```

Thank you 🙂

do i need to set user and password?

i got

> elasticsearch.exceptions.ConnectionError: ConnectionError(\<urllib3.connection.HTTPConnection object at 0x7fe1a2c3c940\>: Failed to establish a new connection: [Errno 61] Connection refused) caused by: NewConnectionError(\<urllib3.connection.HTTPConnection object at 0x7fe1a2c3c940\>: Failed to establish a new connection: [Errno 61] Connection refused)

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 26, 2021, 8:18am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/8 "2021-10-26T08:18:59Z")

</div>

Yep, use something like;

```auto
        'http://user:secret@localhost:9200/',

```

---

<div class="post-metadata">

**Author:** ![razs](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@razs](https://discuss.elastic.co/u/razs)\
**Post date:** [October 26, 2021, 8:36am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/9 "2021-10-26T08:36:30Z")

</div>

no need to add user or pass

```auto
es = Elasticsearch('https://log-viewer.dev/app/logtrail')
print(es) 

res = es.search(index="filebeat-*", query={"match_all": {}})
print("Got %d Hits:" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
    print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

```

> ```
> <Elasticsearch([{'host': 'log-viewer.dev', 'port': 443, 'use_ssl': True, 'url_prefix': '/app/logtrail'}])>
> 
> ```

> ```
> raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
> elasticsearch.exceptions.TransportError: TransportError(301, '')
> 
> ```

Something is missing :s

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 26, 2021, 8:37am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/10 "2021-10-26T08:37:56Z")

</div>

The examples above are not for logtrail, they are for talking directly to Elasticsearch.

---

<div class="post-metadata">

**Author:** ![razs](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@razs](https://discuss.elastic.co/u/razs)\
**Post date:** [October 26, 2021, 11:28am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/11 "2021-10-26T11:28:51Z")

</div>

do you know a way to connect to logtrail then? this is just a log i only need to get the data

---

<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 23, 2021, 11:28am UTC](https://discuss.elastic.co/t/get-search-with-json-request/287616/12 "2021-11-23T11:28:54Z")

</div>

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