# Function started responding with 502s overnight

**URL:** <https://answers.netlify.com/t/function-started-responding-with-502s-overnight/12683>\
**Category:** Support\
**Tags:** lambda-functions\
**Created:** [April 18, 2020, 1:54pm UTC](https://answers.netlify.com/t/function-started-responding-with-502s-overnight/12683 "2020-04-18T13:54:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjatx](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/pjatx/32/9362_2.png) [@pjatx](https://answers.netlify.com/u/pjatx)\
**Post date:** [April 18, 2020, 1:54pm UTC](https://answers.netlify.com/t/function-started-responding-with-502s-overnight/12683/1 "2020-04-18T13:54:22Z")

</div>

I have a simple form that takes data and subscribes someone to a Mailchimp list. It was working fine until someone reported errors overnight (built the form to handle errors and display them to the user).

Turns out the lambdas are still running fine, but started responding with 502 Bad Gateway overnight - seemlingly without any changes that I made.

I’ve tried redeploying a couple of times, but it’s not fixing the problem. I’m constructing responses with 200 status codes in the lambda and those are not making it through, however, the response messages are.

Able to reproduce via postman.

Thinking this is a Netlify issue, but maybe it’s something I’m doing. Pasted my function code below:

Function code below:

```
require('dotenv').config()

const Mailchimp = require('mailchimp-api-v3')

const mailChimpAPI = process.env.MAILCHIMP_API_KEY
const mailChimpListID = process.env.MAILCHIMP_LIST_ID

const mailchimp = new Mailchimp(mailChimpAPI)

exports.handler = function(event, context, callback) {
  const data = JSON.parse(event.body)

  console.log(data)

  if (data.email && data.selectedState) {
    mailchimp
      .post(
        '/lists/' + mailChimpListID + '/members?skip_merge_validation=true',
        {
          email_address: data.email,
          merge_fields: {
            STATE: data.selectedState
          },
          status: 'subscribed'
        }
      )
      .then(function(results) {
        console.log(results)
        const response = {
          statusCode: 200,
          headers: {
            'Access-Control-Allow-Origin': '*', 
            'Access-Control-Allow-Credentials': true 
          },
          body: JSON.stringify({ status: 'Success!' })
        }
        callback(null, response)
      })
      .catch(function(err) {
        console.log(err)

        if (err.title == 'Member Exists') {
          err.detail = 'This email address is already subscribed.'
        }

        const response = {
          statusCode: 200,
          headers: {
            'Access-Control-Allow-Origin': '*', 
            'Access-Control-Allow-Credentials': true 
          },
          body: JSON.stringify({
            status: 'Error',
            title: err.title,
            detail: err.detail
          })
        }
        callback(null, response)
      })
  } else {
    const response = {
      statusCode: 400,
      headers: {
        'Access-Control -Allow-Origin': '*', 
        'Access-Control-Allow-Credentials': true 
      },
      body: JSON.stringify({
        status: 'Error',
        message: 'One or more fields are missing'
      })
    }
    callback(null, response)
  }
}
```

---

<div class="post-metadata">

**Author:** ![fool](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/fool/32/37_2.png) [@fool](https://answers.netlify.com/u/fool)\
**Post date:** [April 20, 2020, 9:50pm UTC](https://answers.netlify.com/t/function-started-responding-with-502s-overnight/12683/2 "2020-04-20T21:50:17Z")

</div>

Hi @pjatx and welcome to our community! Could you give me the exact URL you call the function with, so I can try to correlate with our internal logs? I don’t see any of the sites in your main “pjatx” account as using functions.

---

<div class="post-metadata">

**Author:** ![pjatx](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/pjatx/32/9362_2.png) [@pjatx](https://answers.netlify.com/u/pjatx)\
**Post date:** [April 21, 2020, 10:39pm UTC](https://answers.netlify.com/t/function-started-responding-with-502s-overnight/12683/3 "2020-04-21T22:39:29Z")

</div>

Definitely @fool! It’s under a team account that was provisioned for COVID-19 stuff.

```
https://www.tracetozero.org/.netlify/functions/subscribe

```

Just hit it with postman again and still getting 502’s back. Thanks for looking into it.

---

<div class="post-metadata">

**Author:** ![jen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jen/32/6044_2.png) [@jen](https://answers.netlify.com/u/jen)\
**Post date:** [April 24, 2020, 9:33pm UTC](https://answers.netlify.com/t/function-started-responding-with-502s-overnight/12683/5 "2020-04-24T21:33:52Z")

</div>

Hey @pjatx,  
We dug into this a bit and are seeing this error in our logs:

> “error decoding lambda response: json: cannot unmarshal bool into Go struct field lambdaInvokeResponse.headers of type string”

Some other people seem to have run across a similar error:

> [@Debugging a function returns 502](https://answers.netlify.com/t/debugging-a-function-returns-502/429/):
>
> Hello! I’m trying to debug a netlify function that works correctly locally. The function returns a 502 on netlify and logs the following error: error decoding lambda response: json: cannot unmarshal object into Go struct field lambdaInvokeResponse.body of type string The code for the function: Any idea what’s going on here?

Checking out your response headers and that error message, I would try making `true` a string instead of a boolean. I’m not yet sure if this is an encoding error on our end (as suggested in that other post) but wanted to share the possible fix. Want to give it a shot and let us know how it goes?

---

<div class="post-metadata">

**Author:** ![pjatx](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/pjatx/32/9362_2.png) [@pjatx](https://answers.netlify.com/u/pjatx)\
**Post date:** [April 25, 2020, 2:49pm UTC](https://answers.netlify.com/t/function-started-responding-with-502s-overnight/12683/6 "2020-04-25T14:49:14Z")

</div>

Thanks @jen!

Changing `true` -\> `"true"` in my response headers did the trick.

---

<div class="post-metadata">

**Author:** ![jen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jen/32/6044_2.png) [@jen](https://answers.netlify.com/u/jen)\
**Post date:** [July 2, 2020, 9:30pm UTC](https://answers.netlify.com/t/function-started-responding-with-502s-overnight/12683/7 "2020-07-02T21:30:58Z")

</div>

Update on this: we recently rolled out a change so that we’ll accept both integers and boolean values as header values 🎉
