Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using groovy http-builder in preemptive mode

Tags:

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.

like image 374
Stefan Armbruster Avatar asked Jul 05 '11 20:07

Stefan Armbruster


2 Answers

You can also solve it groovy style with

http = new RESTClient('http://awesomeUrl/') http.headers['Authorization'] = 'Basic '+"myUsername:myPassword".getBytes('iso-8859-1').encodeBase64() 
like image 82
towe75 Avatar answered Sep 23 '22 17:09

towe75


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 
like image 28
Daniel Avatar answered Sep 22 '22 17:09

Daniel