Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Get request to API Gateway using Angular?

I'm trying to hit this AWS endpoint:

https://abcde12345hf.execute-api.us-east-2.amazonaws.com/dev/users

which returns:

[{
  "id": 1,
  "name": "Mike"
},{
  "id": 2,
  "name": "Brian"
}]
  • In my Angular code I'm using AWS4 library to send secretAccessKey, accessKeyId and sessionToken to authenticate the user, but I get the following error:

core.js:12501 ERROR Error: Uncaught (in promise): Error: Request failed with status code 403 Error: Request failed with status code 403

  • Does anyone know how to use AWS4 properly with Angular so I can make this simple GET call? or does anyone know another way to make this call using those keys and token for authentication? Thanks a lot in advance!

This is how I get the keys and token (This part works great)

AWS.config.region = 'us-east-2';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'us-east-2:ehkjthf-sf23-12ds-xxxxxxxxx',
    Logins: {
        'cognito-idp.us-east-2.amazonaws.com/us-east-2_Raxxxx': this.id_token
    }
});

console.log(AWS.config.credentials);

Now, this is how I'm using those keys and token to make the GET call.

(AWS.config.credentials as AWS.Credentials).get(function(error: AWSError){
  if(error){
    console.log('Error ', error);
  }else{
  let request = {
    host: 'https://abcde12345hf.execute-api.us-east-2.amazonaws.com/dev/users',
    method: 'GET',
    url:  'https://abcde12345hf.execute-api.us-east-2.amazonaws.com/dev/users',
    path: '/users',
    headers: {
      "Content-Type":"application/json"
    },
  }

  let signedRequest = aws4.sign(request,
    {
      secretAccessKey: AWS.config.credentials.secretAccessKey,
      accessKeyId: AWS.config.credentials.accessKeyId,
      sessionToken: AWS.config.credentials.sessionToken
    });

    delete signedRequest.headers['Host']
    delete signedRequest.headers['Content-Length']

    axios(signedRequest).then((response) =>{
      console.log(response); // Output the Array Object here
    });
  }
});
like image 496
Devmix Avatar asked Mar 15 '19 17:03

Devmix


People also ask

Can API gateway forward request?

Apps connect to a single endpoint, the API Gateway, that's configured to forward requests to individual microservices.


1 Answers

I got it working and I'll share my solution in case anyone else out there needs it!

I ended up using http service from angular and not axios. Here's my solution;

(AWS.config.credentials as AWS.Credentials).get((error: AWSError) =>{
 if(error){
   console.log('Error ', error);
 }else{
 let request = {
   host: 'abcdefg.execute-api.us-east-2.amazonaws.com',
   method: 'GET',
   url:  `https://abcdefg.execute-api.us-east-2.amazonaws.com/dev/users`,
   path: '/dev/users'
 }

  let signedRequest = aws4.sign(request, {
    secretAccessKey: AWS.config.credentials.secretAccessKey,
    accessKeyId: AWS.config.credentials.accessKeyId,
    sessionToken: AWS.config.credentials.sessionToken
  });
  delete signedRequest.headers['Host'];
  this.http.get(signedRequest.url, signedRequest).subscribe(res => this.myObject = res);
 }
}
like image 53
Devmix Avatar answered Sep 28 '22 16:09

Devmix