# 500 Error from Lambda POST function in React App

**URL:** <https://answers.netlify.com/t/500-error-from-lambda-post-function-in-react-app/48359>\
**Category:** Support\
**Tags:** lambda-functions, create-react-app, netlify-newbie\
**Created:** [December 6, 2021, 12:47pm UTC](https://answers.netlify.com/t/500-error-from-lambda-post-function-in-react-app/48359 "2021-12-06T12:47:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![justuswitmer](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/justuswitmer/32/20587_2.png) [@justuswitmer](https://answers.netlify.com/u/justuswitmer)\
**Post date:** [December 6, 2021, 12:47pm UTC](https://answers.netlify.com/t/500-error-from-lambda-post-function-in-react-app/48359/1 "2021-12-06T12:47:50Z")

</div>

Hello!

I have a Netlify site at [www.icecontractormn.com](https://www.icecontractormn.com). I am a Netlify newbie and a relatively new dev, so I suspect I am missing something basic.

I am trying to build a function that will take data from a form I built and then send it to a third party CMS. The POST I am making keeps throwing a 500 error and I can’t for the life of me figure out what the cause is.

Here is my client side code:

```auto
    axios({
      data: JSON.stringify({
        "customer": {
          "customer_first_name": "John",
          "customer_last_name": "Doe",
          "customer_email": "johndoe@zuper.com",
          "customer_category": "5f1a48d11289d5e82e798167"
        }
      }),
      method: 'POST',
      url: '/.netlify/functions/addCustomer',
    }).then(response => {
      console.log('response.json();', response.json());
      response.json();
    });

```

And here is my function:

```auto
const axios = require("axios");

exports.handler = async function (event, context) {

  // Only allow POST
  if (event.httpMethod !== "POST") {
    return { statusCode: 405, body: "Method Not Allowed" };
  }

  console.log('hit my netlify event.body', event.body);
  return axios({
    method: "POST",
    url: "https://staging.zuperpro.com/api/customers",
    headers: {
      "content-type": "application/json",
      "x-api-key": process.env.ZUPERPRO,
    },
    data: JSON.stringify(event.body),
  }).then((response) => {
    console.log("success", response);
    return {
      statusCode: 200,
      body: JSON.stringify(response)
    }
  }).catch((e) => {
    console.log('got an error', e.message);
    return {
      statusCode: 500,
      body: JSON.stringify({
        error: e.message
      })
    }
  });
};

```

Finally, here are the errors I am seeing.

From the browser:

 ![Screen Shot 2021-12-06 at 6.14.19 AM](https://global.discourse-cdn.com/netlify/original/3X/5/b/5b552f14b14788cda73cd219e8c8c56bb9d01e3c.png)

And from my terminal:

 ![Screen Shot 2021-12-06 at 6.14.06 AM](https://global.discourse-cdn.com/netlify/original/3X/4/c/4c99c8f01bda9cc4e22cd36b6ebf128bc1c4689c.png)

Oh, and while my site is in production this code is still only local.

Any help is appreciated!

Thank you!

---

<div class="post-metadata">

**Author:** ![marcus](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/marcus/32/2651_2.png) [@marcus](https://answers.netlify.com/u/marcus)\
**Post date:** [December 6, 2021, 2:54pm UTC](https://answers.netlify.com/t/500-error-from-lambda-post-function-in-react-app/48359/2 "2021-12-06T14:54:13Z")

</div>

Hey!

It looks like the server you’re calling from your Netlify function is returning the 500 error and the function is just passing it on. Otherwise you wouldn’t see the `got an error` log line.

I suspect this to be the issue:

> [@justuswitmer](#):
>
> ```auto
> data: JSON.stringify(event.body),
> 
> ```

The `event.body` parameter is already a string, since there is no logic to automatically parse JSON inside of it. Your code will quote it a second time. To debug this, consider logging `JSON.stringify(event.body)`.

The fix likely is to remove the `JSON.stringify` and pass the event body directly.

### Security advice

Be careful! Implementing this kind of code will allow anybody to use your function to post to your API, since you’re always injecting the API key. This leaves a lot of room for abuse.

Consider doing one of the following:

- Implement user authentication in your frontend and check in your function that a valid user called your function. You can use [Netlify Identity](https://docs.netlify.com/visitor-access/identity/) and the [user context](https://docs.netlify.com/functions/functions-and-identity/) in a function for that
- At least validate the incoming request to make sure the data being passed on is valid in the context that you want to allow
