gpt4 book ai didi

spring - Grails中的垂直响应API

转载 作者:行者123 更新时间:2023-12-02 14:50:53 25 4
gpt4 key购买 nike

我正在我的grails项目中尝试使用Vertical响应api。但是它给出了以下错误

CODE:

def http = new HTTPBuilder()

http.request('https://vrapi.verticalresponse.com/',GET, TEXT) { req ->
uri.path = 'api/v1/contacts?type=basic'
headers.'Authorization' = 'Bearer api_token'
headers.'Content-Type'='application/json'


print('====================================')
response.success = { resp, reader ->
assert resp.status == 200
println "My response handler got response: ${resp.statusLine}"
println "Response length: ${resp.headers.'Content-Length'}"
reader // print response reader
}

// called only for a 404 (not found) status code:
response.'404' = { resp ->
println 'Not found'
}
}

错误:
Unauthorized. Stacktrace follows:
groovyx.net.http.HttpResponseException: Unauthorized
at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:652)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:441)
at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:417)
...

最佳答案

使用HttpClient而不是HTTPBuilder后,它可以正常工作。所以我认为问题出在HTTPBuilder

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet('http://vrapi.verticalresponse.com/api/v1/contacts?type=basic');

// add request header
request.addHeader("Authorization","Bearer token")

HttpResponse response = client.execute(request);

print("Response Code : "
+ response.getStatusLine().getStatusCode())

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));


render rd.readLines()

关于spring - Grails中的垂直响应API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844881/

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