使用Java中的HttpGet调用APIGateway端点

时间:2018-04-18 17:40:15

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

我试图在Java中使用HttpGet调用APIGateway端点。这是我的代码的样子:

try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            String HOST = "https://<resourceId>.execute-api.us-west-2.amazonaws.com/beta/update";

        /* Prepare get request */
            HttpGet httpGet = new HttpGet(HOST);
        /* Add headers to get request */
            httpGet.addHeader("Content-Type", "application/json");
            httpGet.addHeader("host", HOST);
            TreeMap<String, String> awsHeaders = new TreeMap<String, String>();
            awsHeaders.put("host", HOST);
            AWSV4Auth awsAuth = new AWSV4Auth.Builder("key","value")
                    .regionName("us-west-2")
                    .serviceName("execute-api") // es - elastic search. use your service name
                    .httpMethodName("GET") //GET, PUT, POST, DELETE, etc...
                    .debug()
                    .awsHeaders(awsHeaders)
                    .build();
            Map<String, String> headers = awsAuth.getHeaders();

            for (Map.Entry<String, String> entrySet : headers.entrySet()) {
                httpGet.addHeader(entrySet.getKey(), entrySet.getValue());
            }

            System.out.println("Actual http headers");
            List<Header> getHeaders = Arrays.asList(httpGet.getAllHeaders());
            for (Header header: getHeaders) {
                System.out.println(header.getName() + " : " + header.getValue());
            }

            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            String jsonResponse = EntityUtils.toString(entity, "UTF-8");
            System.out.println("jsonResponse" + jsonResponse);
        } catch (Exception e) {
            System.out.println("Error calling HttpClient");
            e.printStackTrace();
        }
    }

我的APIGateway阶段名称是beta,而methodName是更新。 我在这里使用此GitHub类来签署我的请求 - https://github.com/javaquery/Examples/blob/master/src/com/javaquery/aws/AWSV4Auth.java

我一直看到这个错误 - &#34;这个请求无法满足。&#34; 我使用postman作为参考来生成标题,我仍然无法弄清楚出错的地方或地点。

由于特定的内部问题,我没有尝试使用API​​Gateway生成的SDK。我错过了任何其他需要传入的标题吗?

0 个答案:

没有答案