Amplify-js: API calls missing the response status

Created on 4 Feb 2018  路  8Comments  路  Source: aws-amplify/amplify-js

I am integrating with API calls to APIG and it seems like the response status is missing.
The response object comes back as an empty object.

I am invoking the call as in the official docs

API.post(apiName, path, myInit).then(response => {
    if(response) {
     this.setState({sucess:true})
    } else {
      this.setState({sucess:false})
    }
});

If the request success with a 2XX I can parse the response array just fine.
However, when the request results in an error, there is no response status to parse, e.g. 4xx, 5xx.
I can see in Chrome dev tools that the status codes are indeed present.

Any ideas?

using

  • aws-amplify: 0.2.3
  • aws-amplify-react: 0.1.32
feature-request

Most helpful comment

@powerful23 , I am facing this issue though it is fixed longed back. I have set response:true and calling error.response to get error detail, but getting undefined. Instead getting generic message 'Network Error' in error.message. please help.
image
using:
"@aws-amplify/ui-react": "^0.2.8",
"aws-amplify": "3.0.17-unstable.2",
"aws-amplify-react": "^4.1.15",

All 8 comments

@jsancho 4xx, 5xx can be catched

API.post(apiName, path, myInit)
  .then(response => {
    this.setState({ sucess: true })
  })
  .catch(error => {
    this.setState({ sucess: false })
  });

but still the status code won't be available there :(

@yuyokk this really depends on how your Lambda/API Gateway is setup. If you create an API Gateway as a Lambda proxy, then you would return the status code yourself. This is how the default setup is with awsmobile-cli and using the aws-serverless-express setup.

Did you create the API Gateway / Lambda yourself in your setup, or use mobile hub / cli ?

There are many ways to accomplish this with API Gateway as well, you can see more on that here:
https://docs.aws.amazon.com/apigateway/latest/developerguide/setup-http-integrations.html

@mlabieniec
We've created the API Gateway and Lambda ourselves (originally derived from a mobilehub project)
We are using the {proxy+} integration to handle the calls via aws-serverless-express in lambda.

This is the current config for the APIG Response Method
api gateway

Does that mean that we should add mappings for all http codes as response mappings?

The return object from the API call contains only the "data" attribute from the http request.
(See line 198 in the RestClient file. https://github.com/aws/aws-amplify/blob/master/packages/aws-amplify/src/API/RestClient.ts#L197-L201)

I would prefer having access to the full response object (https://github.com/axios/axios#response-schema).

@jsancho specifically for lambda proxy / api gateway:

Here is what your response should look like for lambda proxy in node.js:
https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format

However, aws-serverless-express will forward your response for you, so you can provide the statusCode in your response object:
https://github.com/awslabs/aws-serverless-express/blob/master/index.js#L60

@BertR we will add returning the entire response as a feature-request thanks!

@mlabieniec
Thanks very much for the tips.
After appending .end() to all responses, the forwarding to APIG kicked in, and Amplify is now parsing the status codes.

Also, thanks for taking onboard the feature request for full responses!

top work 馃憤

@powerful23 , I am facing this issue though it is fixed longed back. I have set response:true and calling error.response to get error detail, but getting undefined. Instead getting generic message 'Network Error' in error.message. please help.
image
using:
"@aws-amplify/ui-react": "^0.2.8",
"aws-amplify": "3.0.17-unstable.2",
"aws-amplify-react": "^4.1.15",

I'm having the same issue has HardikShah, not sure why this issue is close -- error.response is not working as advertised.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shinnapatthesix picture shinnapatthesix  路  3Comments

josoroma picture josoroma  路  3Comments

rygo6 picture rygo6  路  3Comments

DougWoodCDS picture DougWoodCDS  路  3Comments

leantide picture leantide  路  3Comments