# How to pass basic authentication details in http filter plugin

**URL:** <https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246>\
**Category:** Logstash\
**Created:** [September 26, 2019, 1:48pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246 "2019-09-26T13:48:46Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 26, 2019, 1:48pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/1 "2019-09-26T13:48:46Z")

</div>

Hi,  
I am using logstash HTTP filter plugin.  
I want to pass basic authentication details such as username, and password in the request.

Could you please suggest, how we can post a request using header, body, and basic-authentication in every single request?

---

<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:** [September 26, 2019, 3:40pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/2 "2019-09-26T15:40:17Z")

</div>

The http filter has options to supply the body and headers. Basic authentication is just another header.

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 26, 2019, 4:12pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/3 "2019-09-26T16:12:52Z")

</div>

Do i use like below?  
http {  
url =\> "[https://myapi/v1/token](https://myapi/v1/token)"

```
verb => "POST"

  "BasicAuth" => {
	    "Username" =>"xxxxx"
        "Password" => "dddaaaaa"
    }
 

headers => {
"Content-Type" => "application/x-www-form-urlencoded"
}

body => {
"grant_type" => "password"
"username" => "user"
"password" => "password"
"scope" => "myscope"
}

```

}

---

<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:** [September 26, 2019, 4:33pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/4 "2019-09-26T16:33:03Z")

</div>

No, basic authentication is just another header. There is no support in the filter to encode it for you. You would use something like

```
headers => {
    "Content-Type" => "application/x-www-form-urlencoded"
    "Authorization" => "Basic aHR0cHdhdGNoOmY="
}
```

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 26, 2019, 7:41pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/5 "2019-09-26T19:41:53Z")

</div>

Hi ,

Thank you for your response .I have username and password but how to generate the token that is used along with Basic in 'Authorization'.

---

<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:** [September 26, 2019, 8:34pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/6 "2019-09-26T20:34:15Z")

</div>

Step 3 of [this](https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-basic-authentication/) shows you how to encode the string.

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 26, 2019, 9:09pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/7 "2019-09-26T21:09:48Z")

</div>

Hi,

I am using post method and logstash http filter. I was used linux command to convert username:password to base64 encode and used in header section.I am getting error client\_error=\>"undefined method `encoding' for #Hash:0x68246"

filter {

http {  
url =\> "[https://myapi/v1/token](https://myapi/v1/token)"

```
verb => "POST"

headers => {
   "Content-Type" => "application/x-www-form-urlencoded"
    "Authorization" => "Basic Xh4NXidHdONjk="
}
	
body => {
"grant_type" => "password" 
"username" => "username" 
"password" => "password" 
"scope" => "54ff4"
}

```

}  
}

---

<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:** [September 26, 2019, 9:24pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/8 "2019-09-26T21:24:53Z")

</div>

Where are you getting that error? Is it from logstash? From the API? Is there a stack trace?

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 26, 2019, 9:31pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/9 "2019-09-26T21:31:03Z")

</div>

HI ,  
I am getting error in logstash

{  
"host" =\> "Zxxxxx",  
"@timestamp" =\> 2019-09-26T21:26:47.779Z,  
"tags" =\> [  
[0] "\_httprequestfailure"  
],  
"@version" =\> "1",  
"message" =\> "w\r"  
}

---

<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:** [September 26, 2019, 9:45pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/10 "2019-09-26T21:45:09Z")

</div>

I cannot find it in the code, but I do not think it makes sense to supply the body as a hash when you are using a text encoding. If the body were a string then calling String.encode on it would make sense.

What actual text does the API need in the body of the request.

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 26, 2019, 10:00pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/11 "2019-09-26T22:00:53Z")

</div>

I was tested with curl command and its working  
curl --silent --write-out "HTTPSTATUS:%{http\_code}" -H "Authorization: Basic Xh4NXidHdONjk=" -H 'Accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' '[https://myapi/v1/token](https://myapi/v1/token)' -d 'grant\_type'='password' -d 'username'={username} -d 'password'={password} -d 'scope'=${scope})

But when I am using below, it's throwing error

http {  
url =\> "[https://myapi/v1/token](https://myapi/v1/token)"

```auto
verb => "POST"

headers => {
   "Content-Type" => "application/x-www-form-urlencoded"
    "Authorization" => "Basic Xh4NXidHdONjk="
}
	
body => {
"grant_type" => "password" 
"username" => "username" 
"password" => "password" 
"scope" => "54ff4"
}
}
```

---

<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:** [September 26, 2019, 10:03pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/12 "2019-09-26T22:03:31Z")

</div>

Try adding the option

```
body_format => "json"
```

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 26, 2019, 10:33pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/13 "2019-09-26T22:33:03Z")

</div>

No luck. I found other error

filter{  
http {  
url =\> "[https://myapi/v1/token](https://myapi/v1/token)""

```
verb => "POST"

headers => {
   "Content-Type" => "application/x-www-form-urlencoded"
    "Accept" => "application/json"
	"Authorization" => "Basic Xh4NXidHdONjk="
}

```

body =\> {  
"grant\_type" =\> "password"  
"username" =\> "username"  
"password" =\> "password"  
"scope" =\> "54ff4"  
}

body\_format =\>"json"  
target\_body =\> "response\_message"  
}  
}

[2019-09-26T23:27:32,645][ERROR][logstash.filters.http] error during HTTP request {:url=\>"[https://myapi/v1/token](https://myapi/v1/token)", :code=\>400, :response=\>"{"error":"invalid\_request",  
"error\_description":"response\_type or grant\_type is required"}"}  
{  
"message" =\> "h\r",  
"@timestamp" =\> 2019-09-26T22:27:29.583Z,  
"@version" =\> "1",  
"host" =\> "Zxxxxx",  
"tags" =\> [  
[0] "\_httprequestfailure"  
]  
}

---

<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:** [September 26, 2019, 11:06pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/14 "2019-09-26T23:06:47Z")

</div>

OK, so please answer my question about what text the API expects to receive. What text does

```
curl --silent --write-out "HTTPSTATUS:%{http_code}" -H "Authorization: Basic Xh4NXidHdONjk=" -H 'Accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' 'https://myapi/v1/token' -d 'grant_type'='password' -d 'username'={username} -d 'password'={password} -d 'scope'=${scope})

```

generate in the request body. Once you have figured that out, put it in a string in the http filter using

```
body => "your body goes here"
```

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 26, 2019, 11:35pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/15 "2019-09-26T23:35:48Z")

</div>

Request would be like this

`> curl -k -v -i -u <API Key for V3>:<API Secret for V3> 'https://myapi/v3/token' -d 'grant_type=password&username=<User Id>&password=< Password>&scope=<AreaUUID>'`

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [September 27, 2019, 7:36pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/16 "2019-09-27T19:36:04Z")

</div>

How can we send data in http filter that is being send in -d curl command. Lets assume below curl command example and in -d ,curl command sends "payload to send"

```auto
curl -H "Transfer-Encoding: chunked" -d "payload to send" http://example
.com

```

---

<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:** [September 27, 2019, 8:01pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/17 "2019-09-27T20:01:07Z")

</div>

You can use

```
body => "payload to send" body_format => "text"
```

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://discuss.elastic.co/u/saroja)\
**Post date:** [October 3, 2019, 12:50pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/18 "2019-10-03T12:50:02Z")

</div>

It is working find with ruby filter and similar code snippet is used.  
Thank you so much .

With http filter it was really tough my situation so used ruby to get the details.

---

<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:** [October 31, 2019, 12:50pm UTC](https://discuss.elastic.co/t/how-to-pass-basic-authentication-details-in-http-filter-plugin/201246/19 "2019-10-31T12:50:14Z")

</div>

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