Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API.post returns null #6390

Closed
txnico opened this issue Jul 22, 2020 · 17 comments
Closed

API.post returns null #6390

txnico opened this issue Jul 22, 2020 · 17 comments
Assignees
Labels
API Related to REST API issues React Native React Native related issue to-be-reproduced Used in order for Amplify to reproduce said issue

Comments

@txnico
Copy link

txnico commented Jul 22, 2020

Upon upgrading to "aws-amplify": "3.0.20" any call to API.post REST returns null.

To Reproduce
Running managed Expo app with Expo SDK 37
Any call to AWS lambda via API.post.

POST Response: null

Expected behavior
A response object to be returned once promise is complete.

Code Snippet

try {
    const apiName = 'userPreferences';
    const path = "/notifyPref?nType=" + nName + "&nValue=" + nValue;
    const myInit = {
        body: {},
        headers: {'Content-Type': 'application/json'}
    }

    var response = await API.post(apiName,path,myInit);
    console.log("POST Response:", response);
            
    if( !response.match(/Success/) ){
        toastType = 'danger';
        response = "Unable to set preference setting.\n" + response;
    }
            
} catch (err) {
    console.log("Error in userPreference update:",  err);
}
@txnico txnico added the to-be-reproduced Used in order for Amplify to reproduce said issue label Jul 22, 2020
@manueliglesias manueliglesias added the API Related to REST API issues label Jul 22, 2020
@manueliglesias
Copy link
Contributor

manueliglesias commented Jul 22, 2020

Hi @txnico

Are you using your lambda as described here ?

UPDATE:
Which version were you on before when it was working correctly?

@txnico
Copy link
Author

txnico commented Jul 22, 2020

Hi @manueliglesias

I was at aws-amplify@1.1.19 with Expo SDK 35.

I used to have the API exports under: "aws_cloud_logic_custom" block and I've updated them to define them as API.endpoints with no change.

The lambda code is being executed correctly in AWS and the response as far as I can see in API-gateway logs have the expected response object.

Thanks!

@manueliglesias
Copy link
Contributor

Can you share your redacted aws-exports.js or API.endpoints config?

@txnico
Copy link
Author

txnico commented Jul 22, 2020

Sure thing:

const awsmobile =  {
    "aws_project_region": "xxx",
    "aws_content_delivery_bucket": "xxx",
    "aws_content_delivery_bucket_region": "xxx",
    "aws_content_delivery_url": "xxx",
    "aws_cognito_identity_pool_id": "xxx",
    "aws_cognito_region": "xxx",
    "aws_user_pools_id": "xxx",
    "aws_user_pools_web_client_id": "xxx",
    /*
    "aws_cloud_logic_custom": [
        {
            "name": "deviceListRest",
            "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
            "region": "xxx"
        },
        {
            "name": "deviceControl",
            "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
            "region": "xxx"
        },
        {
            "name": "sensorData",
            "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
            "region": "xxx"
        },
        {
            "name": "Notifications",
            "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
            "region": "xxx"
        },
        {
            "name": "userPreferences",
            "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
            "region": "xxx"
        },
        {
            "name": "deviceSchedules",
            "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
            "region": "xxx
        }
    ], */
    //Get rid of: Unhandled promise rejection: Error: No credentials, applicationId or region
    //https://github.com/aws-amplify/amplify-js/issues/5918
    Analytics: {
        disabled: true
    }, 
    
    API: {
        endpoints: [
            {
                "name": "deviceListRest",
                "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
                "region": "xxx"
            },
            {
                "name": "deviceControl",
                "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
                "region": "xxx"
            },
            {
                "name": "sensorData",
                "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
                "region": "xxx"
            },
            {
                "name": "Notifications",
                "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
                "region": "xxx"
            },
            {
                "name": "userPreferences",
                "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
                "region": "xxx"
            },
            {
                "name": "deviceSchedules",
                "endpoint": "https://xxx.execute-api.xxx.amazonaws.com/Prod",
                "region": "xxx"
            }
        ]
    }
};

export default awsmobile;

@elorzafe
Copy link
Contributor

@txnico can you share your package.json and how you are configuring Amplify?

@txnico
Copy link
Author

txnico commented Jul 22, 2020

@elorzafe - here's my package.json. Regarding the configuration, please see aws-exports I posted above.

{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject"
},
"dependencies": {
"@expo/vector-icons": "10.1.0",
"@ptomasroos/react-native-multi-slider": "^2.2.2",
"@react-native-community/netinfo": "^5.9.4",
"aws-amplify": "3.0.10",
"aws-amplify-react-native": "4.1.1",
"expo": "^37.0.0",
"expo-constants": "~9.0.0",
"expo-localization": "~8.1.0",
"expo-permissions": "~8.1.0",
"moment": "^2.24.0",
"native-base": "^2.11.0",
"react": "16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
"react-native-appearance": "~0.3.3",
"react-native-chart-kit": "4.3.0",
"react-native-circular-progress": "^1.1.0",
"react-native-gesture-handler": "~1.6.0",
"react-native-modal-datetime-picker": "^7.6.0",
"react-native-reanimated": "~1.7.0",
"react-native-screens": "^2.9.0",
"react-native-svg": "11.0.1",
"react-navigation": "^4.4.0",
"react-navigation-drawer": "^1.4.0",
"react-navigation-stack": "^1.7.3",
"react-navigation-tabs": "^1.2.0",
"victory-native": "^32.0.1"
},
"devDependencies": {
"babel-preset-expo": "^8.1.0"
},
"private": true
}

@elorzafe
Copy link
Contributor

@txnico I mean are you doing something like this?

import { Amplify, API } from 'aws-amplify';
import awsconfig from './aws-exports';

Amplify.configure(awsconfig);

@txnico
Copy link
Author

txnico commented Jul 22, 2020

@elorzafe:

import Amplify from '@aws-amplify/core';
import Auth from '@aws-amplify/auth';
import { withAuthenticator } from 'aws-amplify-react-native';
import awsmobile from './aws-exports';

Amplify.configure(awsmobile);

@elorzafe
Copy link
Contributor

@txnico where are you importing API?

@txnico
Copy link
Author

txnico commented Jul 22, 2020

@elorzafe - The imports above are in the main app entry-point whereas the API import happens in child components that execute api calls. I'm importing API as:

import { API } from 'aws-amplify';

@txnico
Copy link
Author

txnico commented Jul 29, 2020

Any update or suggestions?

@ashika01
Copy link
Contributor

@txnico Have you tried to import Amplify from "aws-amplify" rather than core?If not could you give that a try, cleaning your node_modules and package lock files?

Something like this,

import Amplify, {API, Auth} from 'aws-amplify';
import { withAuthenticator } from 'aws-amplify-react-native';
import awsmobile from './aws-exports';

Amplify.configure(awsmobile);

@txnico
Copy link
Author

txnico commented Jul 30, 2020

Hi @ashika01 - same behavior unfortunately after clearing out node_modules, package lock files and updating imports.

@ashika01
Copy link
Contributor

@txnico - I think this maybe related #5971

There is some explanation there. which region are you trying this from?

@txnico
Copy link
Author

txnico commented Aug 3, 2020

@ashika01 - Thanks for the suggestion. The API in question returns plain-text JSON responses and both #5971 and the referred CodeGenieApp/serverless-express#99 discuss mime types and compression.

@sammartinez sammartinez added the React Native React Native related issue label Sep 18, 2020
@txnico
Copy link
Author

txnico commented Dec 8, 2020

I revisited this issue this week and found out that the issue was being caused by the lack of serialization in the body of the response object within my lambda function. It must have been a change in AWS API since previous API.post calls with the unedited lambda functions worked previously for months.

@txnico txnico closed this as completed Dec 8, 2020
ashleyoldershaw added a commit to ashleyoldershaw/amplify-cli that referenced this issue Jan 27, 2021
We need a JSON string for the body in order to access it in our response message. See this comment: aws-amplify/amplify-js#6390 (comment)

I had this issue with my own function which was returning null on my JSON.stringify(response). I found the above comment and it made it work! i.e. I could access the return body.
ammarkarachi pushed a commit to aws-amplify/amplify-cli that referenced this issue Mar 19, 2021
We need a JSON string for the body in order to access it in our response message. See this comment: aws-amplify/amplify-js#6390 (comment)

I had this issue with my own function which was returning null on my JSON.stringify(response). I found the above comment and it made it work! i.e. I could access the return body.
@github-actions
Copy link

github-actions bot commented Dec 9, 2021

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Related to REST API issues React Native React Native related issue to-be-reproduced Used in order for Amplify to reproduce said issue
Projects
None yet
Development

No branches or pull requests

5 participants