Call the Create API


#61

@jayair

Thanks very much for your reply jayair. I just found the issue and as usual it was user error, i had an extra whitespace in my config.js for cognito IDENTITY_POOL_ID value. Removing it fixed my issue.

D’OH!


#62

Hi, thanks for your help.
I had the same warning message, and already check my “Identity Pool” setting and is unchecked, I don’t had an extra whitespace in my config.js
I can’t login with a wrong password so I’m logging in correctly, I mean, I think my credentials are good.
I try to post to another URL:

createNote(note) {
    return API.post("notes", "/nontes", {
        body: note
    });
}

but it keeps bringing the same warning message.
What can I do?
Thank you in advance.

(English is not my native language, sorry about that)


#63

Nevermind, my

Amplify.configure

was not correct, I missed to write the line

identityPoolId: config.cognito.IDENTITY_POOL_ID,

Reviewing the lecture “https://serverless-stack.com/chapters/configure-aws-amplify.html” help me.
Sorry to bother you.


#64

No worries. Thanks for following up.


#65

Hi,

I am getting a ‘network error’ alert and 403 error in the console. I’m not quite sure how to proceed. Ah, little help?

Failed to load https://jupiterz3d.execute-api.us-west-2.amazonaws.com/prod/sceneTitles: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.

Also, I am getting INSUFFICIENT_DATA alarms in CloudWatch. They are stating that 15 data points were unkonwn.


#66

When are you seeing this error?


#67

When I click the create button and trigger the API call.

I should add that I am nesting notes in my project so I have one ‘get’ call to pull in the parent note’s id and then the create call saving the data to another table. Do you think that may be causing the CORS error?


#68

Hmm are you making two API calls from your the frontend in that case?


#69

Yeah. The get call from the componentDidMount function and the post call after the user inputs their data and triggers the create API. The ‘Access-Control-Allow-Origin’ header for both is the same. They are both importing the ./libs/response-lib.js file for this.


#70

To simplify debugging this, can you follow this chapter https://serverless-stack.com/chapters/test-the-apis.html to test the two APIs you are trying to call? Just to make sure they are working okay.


#71

Yeah they are working okay. I followed all the great advice that you gave to people also dealing with 403 errors before me.

I’m starting to think that I need to just pass the data in from the previous state instead of calling the get api again. I tried that first but it through some errors and I couldn’t find documentation as clear as yours so I tried it this way.

I think the way I have it set up the browser is assuming it’s a cross-site scripting attack or something.


#72

Hmm a lot of times you get these CORS errors if there is some other kind of error. I would also try to check the API Gateway and Lambda logs to see if there is anything going on.


#73

I’ve been troubleshooting this issue off and on now for two days. The error message I was getting was the following:

No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:3000’ is therefore not allowed access. The response had HTTP status code 403.

I’ve tried just about every single solution posted here but nothing worked. Then when looking at the post from @jayair regarding unauthenticated identities, I remembered the Authenticated and Unauthenticated IAM Roles I created a while ago for this Cognito identity pool. Low and behold, I added a FullAdmin policy to the authenticated role and it worked. So, after looking at both my aws-amplify config, Mobile Hub, Cognito, API Gateway configs, the serverless.yml file, and many sls deploys of the app on each of my many trial-and-error changes, it came down to IAM role permissions. Now to remove that FullAdmin role and follow PoLP access controls. Hope this helps someone out, this drove me crazy!


#74

Yeah debugging is sadly still a very frustrating experience. Glad you figured it out.


#75

If you are getting: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:3000’ is therefore not allowed access. The response had HTTP status code 403.**

Then add this to your handler.js

    headers: {
    "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
    "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS 
   },

credit to this answer -> https://stackoverflow.com/a/43029002

You will have to do a serverless remove and then serverless deploy to get the change to take effect.

Hope this helps someone. Cheers.


#76

Appreciate the tip. Thanks!


#77

I was also running into the “No ‘Access-Control-Allow-Origin’ header is present…” issue and walked through all of the above guidance - didn’t have any issues. Then I realized that I wasn’t actually logged in to the app :expressionless: which may be something other folks run into if you have your local node server running and are refreshing the /notes/new page. Once I logged in, it worked great.