1

I need to get data from an ApiGateway api method. My API is deployed and url is like /greetings. "greetings" is my resource name in which I have a GET method with Authentication set to aws_iam.

The method returns a json response which i want to simply fetch and return. Before authenticating below code was enough to get the response:

String url = "XXXX/greetings"; // xxxx is replaced by api url
URL obj = new URL(url);
con.setRequestMethod("GET");

//add request header
con.setRequestProperty("User-Agent", USER_AGENT);

int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}

in.close();

//print response

Note: I understand that I have to pass Authentication token with the request and I am able to access the authenticated API url with Postman by using my AWS credentials. My query is how do I do this in Java?

Ana
  • 841
  • 10
  • 28

1 Answers1

1

You can generate the Java SDK on your API and use that to call your API. You can set the credentials on the client just like in Postman.

Here is a guide https://aws.amazon.com/blogs/developer/api-gateway-java-sdk/ to generate and use Java SDK with API Gateway.

If for some reason you don't want to use the generated SDK, you can read up on SigV4 signing and do that yourself or use a third party library like the one mentioned here.

Community
  • 1
  • 1
Abhigna Nagaraja
  • 1,874
  • 15
  • 17