# Two Http\_poller inputs with access token

**URL:** https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939
**Category:** Logstash
**Created:** [March 7, 2022, 8:23am UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939 "2022-03-07T08:23:11Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![rodri.gz](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rodri.gz](https://discuss.elastic.co/u/rodri.gz)
#### Post date: [March 7, 2022, 8:23am UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/1 "2022-03-07T08:23:11Z")

</div>

Hello!

I am trying to get data from an api that needs to be accessed using a token that we get from another api.

How can I pass this access token to get the data from the other api?

I had thought of doing something similar to this but I don't know if it's entirely correct.

```auto
input {
  http_poller {
    urls => {
      token => "https://url-token.com/api/token"
    }
    add_field => { "token" => "%{message}" }
    type => "Token-API"
  }

  http_poller {
    urls => {      
      url-api-data => {
        method => get
        cacert => "path/cert.crt"
        user => "x"
        password => "y"
        url => "https:/url.com/api"
        headers => {
          "Content-Type" => "application/json"
          "Authorization" => "Bearer %{token}"
        }
      }
    }
    request_timeout => 60
    schedule => { every => "5s"}
    codec => "json"
    metadata_target => "http_poller_metadata"
    type => "Data-API"
  }
    

}

```

Is there any better way to do it?  
I don't know if in this case there could be data duplication/loss or if it will actually catch the token correctly.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [March 7, 2022, 12:01pm UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/2 "2022-03-07T12:01:50Z")

</div>

> [@rodri.gz](#):
>
> I am trying to get data from an api that needs to be accessed using a token that we get from another api.

You cannot do that using http\_poller. See [this](https://discuss.elastic.co/t/how-to-pass-variable-base-parameter-in-http-poller-logstash-input-plugin/298621/2) for an example and [this](https://discuss.elastic.co/t/how-to-generate-oauth2-token-and-use-rest-api-to-pull-logs-from-cloud/166624/2) for more discussion.

---

<div class="post-metadata">

### Author: ![rodri.gz](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rodri.gz](https://discuss.elastic.co/u/rodri.gz)
#### Post date: [March 7, 2022, 12:25pm UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/3 "2022-03-07T12:25:55Z")

</div>

I don't quite understand why it doesn't work. Is it because the input would not return the token correctly or because the http\_poller does not support tokens?

If I change the first input to get the token to an 'exec' input that curls the url and stores this token in a field and uses it in the poller header, would it work?

or should i keep the token at the input with hhtp\_poller and then with http filter connect to the other api to keep the data?

I'm not very good with ruby ​​and I don't quite understand the example you gave me

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [March 7, 2022, 12:31pm UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/4 "2022-03-07T12:31:23Z")

</div>

The http\_poller input is initialized before any events are created, so it cannot reference fields from them.

---

<div class="post-metadata">

### Author: ![rodri.gz](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rodri.gz](https://discuss.elastic.co/u/rodri.gz)
#### Post date: [March 7, 2022, 1:54pm UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/5 "2022-03-07T13:54:40Z")

</div>

> [@Badger](#):
>
> The http\_poller input is initialized before any events are created, so it cannot reference fields from them.

I'm not understanding it well, I'm sorry for my level of English.

I understand that with the input http\_poller, I get the token and with http filter I get the data of interest from the api?

for example:

```auto
input {
  http_poller {
    urls => {
      token => "https://url-toke.com/api/token"
    }
    type => "Token-API"
  }
}

filter {

  http {
    url => "api/with/data"
    verb => "GET"
    target_body => "field"
    headers {
      "Authorization" => "Bearer %{message}"
    }
  }

}

output{
  elasticsearch{}
}

```

Could you send me a link with some complete example of http filter?

thank you!

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [March 7, 2022, 2:37pm UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/6 "2022-03-07T14:37:22Z")

</div>

> [@rodri.gz](#):
>
> Could you send me a link with some complete example of http filter?

I do not have one.

The schedule option on the http\_poller option is required.

You may be able to use the http\_poller to get the token.

The http filter will only get called when an event goes through the pipeline. That will happen each time the http\_poller is triggered. If you do not want to call the http\_poller input once for every call to the http filter then you must store the token, using code similar to what I shared a link to.

---

<div class="post-metadata">

### Author: ![rodri.gz](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rodri.gz](https://discuss.elastic.co/u/rodri.gz)
#### Post date: [March 7, 2022, 2:52pm UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/7 "2022-03-07T14:52:56Z")

</div>

> [@Badger](#):
>
> I do not have one.
> 
> The schedule option on the http\_poller option is required.
> 
> You may be able to use the http\_poller to get the token.
> 
> The http filter will only get called when an event goes through the pipeline. That will happen each time the http\_poller is triggered. If you do not want to call the http\_poller input once for every call to the http filter then you must store the token, using code similar to what I shared a link to.

i think that the token will be a random one everytime i conect to the api.

I think I'm starting to understand...

I understand that if I want to obtain the data I must perform the http\_poller to obtain the token, once the token is obtained it is stored in the indicated field ( http\_poller\_token\_metadata) , if there is event the http filter is activated and It store the data in the indicated field ( filed-with-data) that will later be indexed in elastic .

Is that so? the data is actually fetched by the http filter? I did not understand well that the filter function is like an input

```auto
input {
  http_poller {
    urls => {
      token-url=> {
        method => get
        cacert => "/path/x.cer"
        user => "user"
        password => "pass"
        url => "http://api-url.com/token"
     }
    }
    request_timeout => 60
    schedule => { every => "5s"}
    metadata_target => "http_poller_token_metadata"
  }
}

filter {

  http {
    url => "api/with/data"
    verb => "GET"
    target_body => "filed-with-data"
    headers {
      "Authorization" => "Bearer %{http_poller_token_metadata}"
    }
  }

}

output{
  elasticsearch{}
}

```

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [March 7, 2022, 5:51pm UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/8 "2022-03-07T17:51:03Z")

</div>

> [@rodri.gz](#):
>
> Is that so? the data is actually fetched by the http filter?

That is correct.

---

<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: [April 4, 2022, 5:51pm UTC](https://discuss.elastic.co/t/two-http-poller-inputs-with-access-token/298939/9 "2022-04-04T17:51:52Z")

</div>

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