2

I'm trying to create an API endpoint using Netlify Lambda Function. The code works perfectly in my local, but always returns Access to XMLHttpRequest at 'https://<my-netlify-project>.netlify.com/.netlify/functions/submit' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

enter image description here

I'm trying to handle OPTIONS and POST in my code, but it doesn't seems working. Here is my code:

const axios = require('axios');

const headers = {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
      'Content-Type': 'application/json',
      'Access-Control-Allow-Methods': '*',
      'Access-Control-Max-Age': 2592000,
      'Access-Control-Allow-Credentials': true,
};

exports.handler = (event, context, callback) => {
      if (event.httpMethod === 'OPTIONS') {
            callback(null, { statusCode: '204', headers });
            return;
      }
      if (event.httpMethod === 'POST') {
            callback(null, {
                  statusCode: 200,
                  body: JSON.stringify({
                        success: true,
                  }),
                  headers,
            });
            return;
      }
};

And I'm trying to call it from a React app, using axios like this:

axios.post('https://<my-netlify-project>.netlify.com/.netlify/functions/test', reqObj)

And I noticed this error appears on my function invocation

10:24:58 PM: error decoding lambda response: json: cannot unmarshal number into Go value of type string

enter image description here

What causes the error and how to resolve it?

Dani Vijay
  • 2,188
  • 2
  • 22
  • 37
  • For best practices, you should include a return callback on failure of both method types. I know this is an example, but just wanted you to be aware. – talves Jan 10 '19 at 17:32

1 Answers1

5

Cors issue

Known issue using localhost to make your call.

The issue during function invocation

The issue is caused by your header values. All values should be strings. The response in the callback expects these values to be strings.

error decoding lambda response: json: cannot unmarshal number into Go value of type string

const headers = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
  'Content-Type': 'application/json',
  'Access-Control-Allow-Methods': '*',
  'Access-Control-Max-Age': '2592000',
  'Access-Control-Allow-Credentials': 'true',
};
talves
  • 13,993
  • 5
  • 40
  • 63
  • Tony, you are killing this topic. I may have to stop monitoring :) – fool Jan 09 '19 at 20:43
  • You guys have taught me well! – talves Jan 09 '19 at 20:46
  • I couldn't understand "They should be strings, so they are added to your response directly". Where I'm doing it wrong and what should I do to resolve this error? @talves – Dani Vijay Jan 10 '19 at 07:20
  • 1
    I made the changes in the above code. Converted the numeric and boolean values to string in `'Access-Control-Max-Age': '2592000'` and `'Access-Control-Allow-Credentials': 'true'`. That solves the error you listed that I quoted above the code. – talves Jan 10 '19 at 14:33