0

I'm trying to call an APIGateway endpoint using a HttpGet in Java. Here's what my code looks like:

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();
        }
    }

My APIGateway stage name is beta and the methodName is update. I'm using this GitHub class here for signing my request - https://github.com/javaquery/Examples/blob/master/src/com/javaquery/aws/AWSV4Auth.java

I always keep seeing this error - "This request could not be satisfied." I used postman as a reference to generate the headers and I still cannot figure out what or where am I going wrong.

I a not trying to use the generated SDK from APIGateway because of a specific internal problem. Am I missing any other headers which need to be passed in?

chrisrhyno2003
  • 3,906
  • 8
  • 53
  • 102
  • Where in the headers to you put credentials like `secretAccessKey` and `accessKeyID`? – jny Apr 23 '18 at 15:31
  • I pass the accessKey and secreyKey to the AWSV4Auth class. I've not added them in the code base here since they're sensitive fields.The fields "key" and "value" in the AWSV4Auth builder are where the accessKeys and SecreKeys would go in. – chrisrhyno2003 Apr 23 '18 at 16:03
  • From looking around (for example, here: https://stackoverflow.com/questions/20664018/cloudfront-custom-origin-distribution-returns-502-error-the-request-could-not-b) it looks like there is an issue with ssl certificates. – jny Apr 23 '18 at 18:30

0 Answers0