using groovy http-builder in preemptive mode
Asked Answered
K

3

22

When using groovy's http-builder with basic authentication the default behavior is to send an unauthenticated request first and resend the request with credentials after receiving a 401 in the first place. Apache's Httpclient offers preemptive authentication to send the credentials directly on the first request. How can I use preemptive auth in Groovy's http-builder? Any code examples are appreciated.

Kliman answered 5/7, 2011 at 20:7 Comment(0)
M
35

Based on a JIRA issue you can do something like that :

def http = new RESTClient('http://awesomeUrl/')

http.client.addRequestInterceptor(new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, HttpContext httpContext) {
        httpRequest.addHeader('Authorization', 'Basic ' + 'myUsername:myPassword'.bytes.encodeBase64().toString())
    }
})

def response = http.get(path: "aResource")

println response.data.text
Moore answered 10/7, 2011 at 15:38 Comment(1)
beautiful. I was getting errant (and seemingly harmless, but annoying) 500 errors with Spring Security Grails plugin and basic auth without preemptive mode set. This fixed it. Note I had to add the following imports: import org.apache.http.HttpRequestInterceptor import org.apache.http.protocol.HttpContext import org.apache.http.HttpRequestBlinnie
Q
41

You can also solve it groovy style with

http = new RESTClient('http://awesomeUrl/')
http.headers['Authorization'] = 'Basic '+"myUsername:myPassword".getBytes('iso-8859-1').encodeBase64()
Quasimodo answered 7/3, 2013 at 20:16 Comment(2)
Also, getBytes('iso-8859-1') can be replaced by simply bytes.Neomaneomah
@tweber could you please clarify: what is the purpose of the encoding? is it somehow required by base64? or by the Basic auth protocol? Edit: hmm, this answer seems to address my question. Haven't wrapped my head around it yet though. Any answer would be appreciated.Ultravirus
M
35

Based on a JIRA issue you can do something like that :

def http = new RESTClient('http://awesomeUrl/')

http.client.addRequestInterceptor(new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, HttpContext httpContext) {
        httpRequest.addHeader('Authorization', 'Basic ' + 'myUsername:myPassword'.bytes.encodeBase64().toString())
    }
})

def response = http.get(path: "aResource")

println response.data.text
Moore answered 10/7, 2011 at 15:38 Comment(1)
beautiful. I was getting errant (and seemingly harmless, but annoying) 500 errors with Spring Security Grails plugin and basic auth without preemptive mode set. This fixed it. Note I had to add the following imports: import org.apache.http.HttpRequestInterceptor import org.apache.http.protocol.HttpContext import org.apache.http.HttpRequestBlinnie
E
0

Used following with Jenkins.

def accessToken = "ACCESS_TOKEN".bytes.encodeBase64().toString()
def req = new URL("https://raw.githubusercontent.com/xxxx/something/hosts").openConnection();
req.setRequestProperty("Authorization", "Basic " + accessToken)
def content = req.getInputStream().getText()
Edp answered 10/9, 2022 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.