Ste*_*ter 21 authentication groovy httpbuilder
当使用具有基本身份验证的groovy的http-builder时,默认行为是首先发送未经身份验证的请求,并在首先收到401后使用凭据重新发送请求.Apache的Httpclient提供抢先身份验证,以便在第一次请求时直接发送凭据.如何在Groovy的http-builder中使用preemptive auth?任何代码示例都表示赞赏.
小智 33
基于JIRA问题,您可以执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
小智 32
你也可以用它来解决它的时髦风格
http = new RESTClient('http://awesomeUrl/')
http.headers['Authorization'] = 'Basic '+"myUsername:myPassword".getBytes('iso-8859-1').encodeBase64()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14233 次 |
最近记录: |