Query the HTTP response code using the AWS API Gateway JS SDK

I encountered a problem while using the JS SDK generated by API Gateway. Whenever there are errors, a CORS error occurs and I am unable to access the data or status code:

https://i.sstatic.net/9b4QT.png

Even though there is a message in the network tab, the data appears as ""

https://i.sstatic.net/d2ZnA.png

This is the code snippet I am working with:

AWS.config.region = "ap-northeast-2"
const cognitoParams = {
  IdentityPoolId: "ap-northeast-2:...",
  Logins: {
    "accounts.google.com": googleUser.getAuthResponse().id_token
  }
}

AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

AWS.config.credentials.get(function () {

  const api = apigClientFactory.newClient({
    accessKey: AWS.config.credentials.accessKeyId,
    secretKey: AWS.config.credentials.secretAccessKey,
    sessionToken: AWS.config.credentials.sessionToken
  })
  api.ec2Get()
    .then(function (data) {
      console.log('data', data);
    })
    .catch(function (err) {
      console.error('err', err)
    })

Answer №1

Make sure to also include the region within your code:

const api = apigClientFactory.newClient({
  accessKey: AWS.config.credentials.accessKeyId,
  secretKey: AWS.config.credentials.secretAccessKey,
  sessionToken: AWS.config.credentials.sessionToken,
  region: '...'
})

If you omit it, the default region used is 'us-east-1'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with browser timeouts for HTTP requests using JavaScript

Managing time out situations in a web application when calling a REST API is crucial. Below is the code snippet I am using to make the API call with jQuery ajax. $.ajax({ type: "POST", url: endpoint, data: payload, ...

Leveraging numerous identifiers in jQuery

I created a small jQuery script to check if the input box value is greater than 5, but I have two tags with IDs and only one of them seems to be working. <div id="register"> <form id="register"> <input id="first" type="text" /> <a ...

send the value of a variable from a child component to its parent

I have created a typeahead component within a form component and I am trying to pass the value of the v-model from the child component to its parent. Specifically, I want to take the query model from the typeahead component and place it in the company mode ...

Creating a component context in React JS

Can someone help me understand how to create a React component context? I have managed to get it working, but I struggle because of my lack of understanding of React. I will share my code. My goal is to create a set of 4 page components that appear withi ...

Grunt: The target file will have the same name as the source file once it has been templ

I am attempting to template all files within a particular directory and save the output in the bin/admin folder, where each destination file will have the same name as its corresponding source file. However, I am facing an issue with specifying the dest in ...

What is the best way to locate a particular item in the JSON file for Our World in Data's COVID-19 Master Vaccinations dataset?

As a new coder, I am currently working on accessing specific elements within the COVID Vaccine JSON file from Our World in Data. However, I'm struggling with the syntax of my code and the structure of the JSON file. I have managed to retrieve data fr ...

Mistakes in design when transforming inline SVG to PNG format

One of my main objectives is to convert a <div> element that contains multiple inline svg images into a png file. The entire process must be carried out within the client's browser using JavaScript. I have experimented with various techniques: ...

Ways to output a string array from a JSON object containing additional attributes

On the client side, I have received a JSON object from a REST service. This object contains multiple attributes, one of which is a String array. I need guidance on how to display this array using embedded AngularJS code in HTML. Here is my JSON object: [ ...

Choosing a specific dropdown option by using xpath (or a more efficient method)

How can I specifically choose an element from the drop down menu? <li role="presentation" ng-repeat="match in $matches" ng-class="{active: $isActive($index)}" class="ng-scope active"> <a style="cursor: default" role="menuitem" tabindex="-1" n ...

What is the proper way to safely close the pg-promise connection in a node.js environment once all jest tests are completed?

I am facing a challenge in closing a PG-Promise database connection after running a function test in Jest. The database connection is initialized in one central location (db.js) and required in multiple places. In this scenario, it is being used by seed.j ...

Is there a way to identify when an image extends beyond the boundaries of the browser window and subsequently reposition it?

Currently, I am implementing a method found at this link to display popup images of my thumbnails. However, there is an issue when the thumbnail is positioned close to the edge of the screen and the original image is too large, causing it to be cut off by ...

I was surprised to find something other than a gulp folder in the node_modules directory after running npm install gulp --save-dev

Seeking some guidance on my process - can someone point out if I am making a mistake? Downloaded Node from their official website Global installation of Gulp (npm install --global gulp) Created a folder and initiated it by running (npm init), which gene ...

Package an npm module and transform it into an ECMAScript module

I'm trying to incorporate the chessground library into one of my projects. It appears to be a CommonJS module, so I utilized browserify to bring it into my web page. browserify -r chessground > chessground.js To make use of it on my webpage, I am ...

Retrieving numerical values from strings using JavaScript

The text I have is formatted as follows: let str = "url(#123456)"; Within the given string, there is a number embedded in it. This number could appear anywhere in the string. I am looking to extract the number 123456 from the provided text. My current ...

What is the process for choosing with multiple attribute selectors?

Is there a way to disable multiple fields in a checkbox survey simultaneously? I attempted the following code, but it only works with one class. Is it possible to select by multiple classes within the same div? function surveyInit(){ $("div[class*=&apos ...

Import an array of dynamic modules with Webpack that are known during compilation

For my project, I have the requirement to import specific modules whose actual paths are only known during compile time. Imagine having components/A.js, components/B.js, and components/C.js. In my App.js, I need to include a subset of these modules that w ...

Explore the jQuery feature that allows you to hover over text

I'm facing an issue that has got me a bit stuck on how to resolve it. Currently, when hovering over this specific image, the opacity decreases (exposing a background color) and text appears. However, I want this effect to persist even when hovering o ...

The browser is preventing files from being accessed through Express because they do not have the text/html MIME type

Currently, I am attempting to set up a nodejs express web server with a static frontend. In order to handle the GET requests made to /, I have implemented myServer.use(express.static("public"));. Within the public folder are HTML, JavaScript, CSS, and im ...

Choose several locations recommended by Google Places

Looking to implement a multiple select feature on v-select to allow users to select one or more cities suggested by Google. Has anyone successfully done this before? I haven't been able to find any examples. https://vue-select.org/ ...

Having trouble with the "Corrupted @import" error during grunt build?

As I embark on my journey to create my very first Angular application, I have encountered a roadblock. Using Yeoman and angular-generator, everything seemed to be running smoothly with "grunt serve." However, when I attempted to execute "grunt build," the ...