# Response.code:401

**URL:** https://discuss.elastic.co/t/response-code-401/141359
**Category:** Logstash
**Created:** [July 24, 2018, 10:48am UTC](https://discuss.elastic.co/t/response-code-401/141359 "2018-07-24T10:48:42Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![m.sereda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.sereda/32/48232_2.png) [@m.sereda](https://discuss.elastic.co/u/m.sereda)
#### Post date: [July 24, 2018, 10:48am UTC](https://discuss.elastic.co/t/response-code-401/141359/1 "2018-07-24T10:48:42Z")

</div>

Hi,  
I am trying to get to the endpoints but getting an error `response.code:401`. Could anyone explain me how to fix this?

---

<div class="post-metadata">

### Author: ![shaunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaunak/32/6643_2.png) [@shaunak](https://discuss.elastic.co/u/shaunak)
#### Post date: [July 25, 2018, 12:29pm UTC](https://discuss.elastic.co/t/response-code-401/141359/3 "2018-07-25T12:29:29Z")

</div>

It looks like httpbeat is having trouble authenticating when it makes the `GET http://localhost:8080/api/dashboard/staging-space` request every 10 seconds.

Without running httpbeat at all, could you try using your browser in private browsing mode to make a `GET` request to `http://localhost:8080/api/dashboard/staging-space`? Do you get prompted for authentication? Is it a basic auth prompt?

---

<div class="post-metadata">

### Author: ![shaunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaunak/32/6643_2.png) [@shaunak](https://discuss.elastic.co/u/shaunak)
#### Post date: [July 25, 2018, 2:34pm UTC](https://discuss.elastic.co/t/response-code-401/141359/5 "2018-07-25T14:34:20Z")

</div>

Yeah, that's what I meant. Thanks.

In the screenshot it says "session has expired". Could you make the same request using `curl -v http://localhost:8080/api/dashboards/staging-space` from the command line and paste the complete output here?

---

<div class="post-metadata">

### Author: ![m.sereda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.sereda/32/48232_2.png) [@m.sereda](https://discuss.elastic.co/u/m.sereda)
#### Post date: [July 25, 2018, 2:56pm UTC](https://discuss.elastic.co/t/response-code-401/141359/7 "2018-07-25T14:56:21Z")

</div>

I have change my httpbeat.yml like the following

---

<div class="post-metadata">

### Author: ![m.sereda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.sereda/32/48232_2.png) [@m.sereda](https://discuss.elastic.co/u/m.sereda)
#### Post date: [July 25, 2018, 4:18pm UTC](https://discuss.elastic.co/t/response-code-401/141359/8 "2018-07-25T16:18:14Z")

</div>

also tried with http\_poller but the result is the same `response.code: 401`

```
input {
  http_poller {
    urls => {
      test2 => {
        url => "http://localhost:8080/api/dash/backup"
        method => "get"
        headers => {
          Accept => "application/json"
        }
        auth => {
          user => "admin"
          password => "pass"
        }
     }
    }
    request_timeout => 60
    # Supports "cron", "every", "at" and "in" schedules by rufus scheduler
    schedule => { cron => "* * * * * UTC"}
    codec => "json"
    # A hash of request metadata info (timing, response headers, etc.) will be sent here
    metadata_target => "http_poller_metadata"
  }
}

output {
  stdout {
    codec => rubydebug
  }
}
```

---

<div class="post-metadata">

### Author: ![shaunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaunak/32/6643_2.png) [@shaunak](https://discuss.elastic.co/u/shaunak)
#### Post date: [July 25, 2018, 5:03pm UTC](https://discuss.elastic.co/t/response-code-401/141359/9 "2018-07-25T17:03:37Z")

</div>

Sorry, the URL in my curl request was bad. Here is the correct curl request to make:

```auto
curl -v http://localhost:8080/api/dashboard/staging-space

```

> I changed it because i know that firstly i need to login and after that check another endpoints. Does any agent that makes a request to APIs to use cookies?

I see. Given this, I would suggest trying the following:

1. Open your browser's developer tools so you can inspect HTTP requests and responses, specifically their headers.
2. Login as you usually do via the browser.
3. Inspect the login response headers, and look for the `Set-Cookie` header. Copy it's value.
4. Edit your `httpbeat.yml` and under the `headers` section, add a line: `Cookie: <value from previous step>`
5. Try to use `httpbeat` now.

Similarly, if you used the Logstash `http_poller` input instead, it has a `headers` setting as well. You can see it in this example here: [Http\_poller input plugin | Logstash Reference [8.11] | Elastic](https://www.elastic.co/guide/en/logstash/current/plugins-inputs-http_poller.html#_example).

---

<div class="post-metadata">

### Author: ![m.sereda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.sereda/32/48232_2.png) [@m.sereda](https://discuss.elastic.co/u/m.sereda)
#### Post date: [July 26, 2018, 6:09am UTC](https://discuss.elastic.co/t/response-code-401/141359/10 "2018-07-26T06:09:41Z")

</div>

ok, here is the output, sorry for late answer

```
curl -v http://localhost:8080/api/dash/space
* About to connect() to localhost port 8080 (#0)
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /api/dashboard/staging-space HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 401 Unauthorized
< Server: Payara Micro #badassfish
< Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, OPTIONS
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: accept, origin, content-type, cookie, x-requested-with, x-http-method-override, authorization, If-Modified-Since, 
< Content-Type: application/json;charset=UTF-8
< Date: Thu, 26 Jul 2018 06:07:47 GMT
< Content-Length: 63
< X-Frame-Options: SAMEORIGIN
< 
* Connection #0 to host localhost left intact
{"errorCode":"SESSION_EXPIRED","message":"Session has expired"}
```

---

<div class="post-metadata">

### Author: ![m.sereda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.sereda/32/48232_2.png) [@m.sereda](https://discuss.elastic.co/u/m.sereda)
#### Post date: [July 26, 2018, 10:18am UTC](https://discuss.elastic.co/t/response-code-401/141359/13 "2018-07-26T10:18:55Z")

</div>

> Open your browser's developer tools so you can inspect HTTP requests and responses, specifically their headers.  
> Login as you usually do via the browser.  
> Inspect the login response headers, and look for the Set-Cookie header. Copy it's value.  
> Edit your httpbeat.yml and under the headers section, add a line: Cookie:   
> Try to use httpbeat now.

OK, I've made it but with http\_poller and it is working  
My logstash.conf is the following now

```
input {
  http_poller {
    urls => {
      test2 => {
        url => "http://localhost:8080/api/dash/space"
        method => "get"
        headers => {
          Accept => "application/json"
          Cookie => "JSESSIONID=5fad76f01bbed6f502dd68095e18"
        }
     }
    }
    request_timeout => 60
    # Supports "cron", "every", "at" and "in" schedules by rufus scheduler
    schedule => { cron => "* * * * * UTC"}
    codec => "json"
    # A hash of request metadata info (timing, response headers, etc.) will be sent here
    metadata_target => "http_poller_metadata"
  }
}
output {
  stdout {
    codec => rubydebug
  }
}

```

And now question how to renew the Cookie for example every 5 minutes automatically? For example if will make like this

```
input {
  http_poller {
    urls => {
      test1 => {
        url => "http://localhost:8080/api/session/login"
        method => "post"
        headers => {
          Accept => "application/json"
        
        }

      test2 => {
        url => "http://localhost:8080/api/dashboard/staging-space"
        method => "get"
        headers => {
          Accept => "application/json"
          Cookie => "JSESSIONID=5fad76f01bbed6f502dd68095e18"
        }
     }
    }

```

and test2 will automatically take cookie from session/login  
My login is used a JSON file with such input

```
{
    "login": "{{ adminLogin }}",
    "password": "{{ adminPass }}"
}

```

---

<div class="post-metadata">

### Author: ![shaunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaunak/32/6643_2.png) [@shaunak](https://discuss.elastic.co/u/shaunak)
#### Post date: [July 27, 2018, 4:34pm UTC](https://discuss.elastic.co/t/response-code-401/141359/14 "2018-07-27T16:34:55Z")

</div>

Hmm, I'm not sure how to accomplish the scenario you described (re-authenticate periodically to fetch a new cookie and then use that in you second request). I'm going to move this post to the Logstash forum to see if anyone might have an idea there.

---

<div class="post-metadata">

### Author: ![m.sereda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.sereda/32/48232_2.png) [@m.sereda](https://discuss.elastic.co/u/m.sereda)
#### Post date: [July 27, 2018, 6:44pm UTC](https://discuss.elastic.co/t/response-code-401/141359/16 "2018-07-27T18:44:05Z")

</div>

Could you please tell me also how to make a POST request with JSON ?

---

<div class="post-metadata">

### Author: ![m.sereda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.sereda/32/48232_2.png) [@m.sereda](https://discuss.elastic.co/u/m.sereda)
#### Post date: [July 27, 2018, 6:44pm UTC](https://discuss.elastic.co/t/response-code-401/141359/17 "2018-07-27T18:44:19Z")

</div>

Yes, it is exactly what I want. Or maybe there is another way how to do that ?

---

<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: [August 24, 2018, 6:44pm UTC](https://discuss.elastic.co/t/response-code-401/141359/18 "2018-08-24T18:44:30Z")

</div>

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