<分区>
我正在从 Java 控制台应用程序调用以下 AWS Gateway API。在 postman 中,它与 AWS 签名完美配合。
获取 https://api.valorebooks.com/bid?isbns=[ “9780026840019”]
我正在使用 AWS SDK 和这个库。 https://github.com/rpgreen/apigateway-generic-java-sdk
控制台应用程序传递参数 isbns 但 API 抛出 HTTP:400
<分区>
我正在从 Java 控制台应用程序调用以下 AWS Gateway API。在 postman 中,它与 AWS 签名完美配合。
获取 https://api.valorebooks.com/bid?isbns=[ “9780026840019”]
我正在使用 AWS SDK 和这个库。 https://github.com/rpgreen/apigateway-generic-java-sdk
控制台应用程序传递参数 isbns 但 API 抛出 HTTP:400
最佳答案
将参数转换为 json 字符串并将该字符串作为参数发布到查询字符串
Map<String, String> parameters = new LinkedHashMap<>();
List<String> productsIsbs = Arrays.asList(request.getProducts())
.stream().map(x -> x.getIsbn()).collect(Collectors.toList());
String params = new Gson().toJson(productsIsbs).replace("\"", "\'");
parameters.put("isbns", params);
GenericApiGatewayRequest apiGatewayRequest;
try {
apiGatewayRequest = new GenericApiGatewayRequestBuilder()
.withHeaders(headers)
.withHttpMethod(HttpMethodName.GET)
.withResourcePath("/bid")
.withParameters(parameters)
.build();
GenericApiGatewayApacheResponse response = client.executeGetWithHttpClient(apiGatewayRequest);
if(response.getHttpResponse().getStatusLine().getStatusCode() == 200) {
String responseJson = response.getBody();
}
} catch (GenericApiGatewayException e) { // exception thrown for any non-2xx response
System.out.println(String.format("Client exception:%s - %s", e.getStatusCode(), e.getMessage()));
e.printStackTrace();
}
关于java - 调用 AWS API Gateway,GET 调用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48548097/