调用AWS API Gateway,发出GET调用问题

时间:2018-01-31 17:29:30

标签: java amazon-web-services aws-sdk aws-api-gateway

我在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

源API:https://valorebooks.github.io/api/source/bid/

2 个答案:

答案 0 :(得分:0)

将参数转换为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();
        }

答案 1 :(得分:-1)

你有没有试过这样的GET?

https://api.valorebooks.com/bid?isbns=9780026840019

在查询参数中有不同的表示列表的方法。您的API网关可能未正确配置。