# Http\_poller REST API authentication token

**URL:** https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870
**Category:** Logstash
**Created:** [August 30, 2020, 9:40am UTC](https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870 "2020-08-30T09:40:44Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![joe\_good](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@joe\_good](https://discuss.elastic.co/u/joe_good)
#### Post date: [August 30, 2020, 9:40am UTC](https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870/1 "2020-08-30T09:40:44Z")

</div>

Hello,  
I'm trying to use logstasth http\_poller plugin to get some events from a rest API method.  
Before calling the events API methods, I should call the login method and get a token from its response. While I use the events API, I have to provide the token I just got from the login method.  
Can I have some example how to do it?

---

<div class="post-metadata">

### Author: ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)
#### Post date: [August 30, 2020, 11:19am UTC](https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870/2 "2020-08-30T11:19:42Z")

</div>

Here is an example

```
input {
  http_poller {
    urls => {
      my-api-poller => {
        method => get
		cacert => "/home/my-user/my-ssl.io.cert.cer"
		pool_max => 1
		pool_max_per_route => 1
		keepalive => true
		automatic_retries => 1
		socket_timeout => 60
        url => "https://my-api-url"
        headers => {
          Accept => "application/json"
		  Authorization => "Bearer my-token"
        }
     }
    }
    request_timeout => 60
	schedule => { every => "15m"}
    codec => "json"
    metadata_target => "my-assets"
  }
}
```

---

<div class="post-metadata">

### Author: ![joe\_good](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@joe\_good](https://discuss.elastic.co/u/joe_good)
#### Post date: [August 30, 2020, 1:26pm UTC](https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870/3 "2020-08-30T13:26:34Z")

</div>

thanks Yassine,  
I have two methods to call:

1. Login method - which is givving me the token and its reponse headers. and then call the second method
2. GetEvents method - which is needed for the token from the Login method.

At your example:

1. Where is the login method call?
2. Where is the GetEvents method should be placed?
3. How can I pass the token from the login call to the GetEvents call?
4. 'my-token' - Am I need to use it somewhere?

---

<div class="post-metadata">

### Author: ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)
#### Post date: [August 30, 2020, 1:54pm UTC](https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870/4 "2020-08-30T13:54:33Z")

</div>

in this case, you can use the input plugin http\_poller to get the token  
then use that token with the [filter plugin http](https://www.elastic.co/guide/en/logstash/current/plugins-filters-http.html) to get your events

---

<div class="post-metadata">

### Author: ![joe\_good](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@joe\_good](https://discuss.elastic.co/u/joe_good)
#### Post date: [August 30, 2020, 2:01pm UTC](https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870/5 "2020-08-30T14:01:35Z")

</div>

thanks. Any example for this solution?

---

<div class="post-metadata">

### Author: ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)
#### Post date: [August 30, 2020, 2:07pm UTC](https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870/6 "2020-08-30T14:07:12Z")

</div>

The input use http\_poller for authentification and getting a token

```
input {
  http_poller {
    urls => {
      my-api-poller => {
        method => get
		cacert => "/home/my-user/my-ssl.io.cert.cer"
		pool_max => 1
		pool_max_per_route => 1
		keepalive => true
		automatic_retries => 1
		socket_timeout => 60
        url => "https://my-api-url"
        headers => {
          Accept => "application/json"
		  Authorization => "Basic username:password"
        }
     }
    }
    request_timeout => 60
	schedule => { every => "15m"}
    codec => "json"
    metadata_target => "my-token"
  }
}

```

In the filter section of your LS config, you need to access the field `my-token` et get the token from the response  
Then use this token as part of` http filter`

```
http {
  body_format => "json"
  follow_redirects => false
  body => {
    "attribute" => "%{[attribute_value]}"
  }
  url => "my-api-url/GetEvents"
  verb => "POST" --- or GET !
  headers => ["Authorization", "Bearer ${My-token}"]
  target_body => "[@metadata][api_response]"
  target_headers => "[@metadata][api_headers]" 
}

```

...

Hope this can help

---

<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: [September 27, 2020, 2:07pm UTC](https://discuss.elastic.co/t/http-poller-rest-api-authentication-token/246870/7 "2020-09-27T14:07:28Z")

</div>

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