在抢占模式下使用groovy http生成器

z9smfwbn  于 2022-09-21  发布在  其他
关注(0)|答案(3)|浏览(101)

在使用带基本身份验证的Groovy的http-Builder时,默认行为是首先发送一个未经身份验证的请求,然后在收到401之后重新发送带有凭据的请求。ApacheHttpClient提供抢占式身份验证,以便在第一次请求时直接发送凭据。如何在Groovy的http构建器中使用抢占式身份验证?欢迎使用任何代码示例。

qojgxg4l

qojgxg4l1#

你也可以用一种时尚的方式来解决它

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

0sgqnhkj2#

基于JIRA issue,您可以执行以下操作:

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
zysjyyx4

zysjyyx43#

与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()

相关问题