using groovy http-builder in preemptive mode

Stefan Armbruster picture Stefan Armbruster · Jul 5, 2011 · Viewed 16.3k times · Source

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.

Answer

towe75 picture towe75 · Mar 7, 2013

You can also solve it groovy style with

http = new RESTClient('http://awesomeUrl/')
http.headers['Authorization'] = 'Basic '+"myUsername:myPassword".getBytes('iso-8859-1').encodeBase64()