gpt4 book ai didi

groovy - 为什么 HTTPBuilder 基本身份验证不起作用?

转载 作者:行者123 更新时间:2023-12-04 01:14:39 27 4
gpt4 key购买 nike

以下代码不会对用户进行身份验证(没有发生身份验证失败,但由于缺乏权限导致调用失败):

def remote = new HTTPBuilder("http://example.com")
remote.auth.basic('username', 'password')
remote.request(POST) { req ->
uri.path = "/do-something"
uri.query = ['with': "data"]

response.success = { resp, json ->
json ?: [:]
}
}

但以下工作正常:
def remote = new HTTPBuilder("http://example.com")
remote.request(POST) { req ->
uri.path = "/do-something"
uri.query = ['with': "data"]
headers.'Authorization' =
"Basic ${"username:password".bytes.encodeBase64().toString()}"

response.success = { resp, json ->
json ?: [:]
}
}

为什么第一个不起作用?

最佳答案

我能想到的两件事。.setHeaders方法需要一张 map 。你试过了吗'Authorization' : "Basic ${"username:password".bytes.encodeBase64().toString()}" ?

如果没有,它需要更多的工作和代码,但您可以使用 URIBuilder以及。一般我封装到不同的类

protected final runGetRequest(String endpointPassedIn, RESTClient Client){
URIBuilder myEndpoint = new URIBuilder(new URI(Client.uri.toString() + endpointPassedIn))
HttpResponseDecorator unprocessedResponse = Client.get(uri: myEndpoint) as HttpResponseDecorator
def Response = unprocessedResponse.getData() as LazyMap
return Response
}

希望这有帮助

关于groovy - 为什么 HTTPBuilder 基本身份验证不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19456670/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com