# Debugging a function returns 502

**URL:** <https://answers.netlify.com/t/debugging-a-function-returns-502/429>\
**Category:** Support\
**Tags:** lambda-functions\
**Created:** [April 12, 2019, 11:20pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429 "2019-04-12T23:20:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtleniger](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jtleniger/32/321_2.png) [@jtleniger](https://answers.netlify.com/u/jtleniger)\
**Post date:** [April 12, 2019, 11:20pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/1 "2019-04-12T23:20:29Z")

</div>

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:

> <https://gist.github.com/jtleniger/d2dc2bc33cdf418076e37cb5420c5a41>

Any idea what’s going on here?

---

<div class="post-metadata">

**Author:** ![DavidWells](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/davidwells/32/22_2.png) [@DavidWells](https://answers.netlify.com/u/DavidWells)\
**Post date:** [April 13, 2019, 12:11am UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/3 "2019-04-13T00:11:21Z")

</div>

Hey @jtleniger

This looks like a possible encoding bug on our end.

What does your function return. Aka what is `result` on line 149

```auto
 const result = distance(geo1.lat, geo1.lng, geo2.lat, geo2.lng);

    callback(null, {
        statusCode: 200,
        body: result
    });

```

Thanks for the report!

---

<div class="post-metadata">

**Author:** ![jtleniger](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jtleniger/32/321_2.png) [@jtleniger](https://answers.netlify.com/u/jtleniger)\
**Post date:** [April 13, 2019, 12:24am UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/4 "2019-04-13T00:24:08Z")

</div>

Hey David!

Thanks for the quick response. I think you may be right, it doesn’t appear to be something wrong with my instance as the hello world example works correctly.

On my local machine, I’ve logged the result parameter to callback and it’s the following:

```
{ statusCode: 200, body: 1533.0575706224508 }

```

The other thing I don’t know how to debug but am suspicious of is some sort of error I’m not handling getting placed in the body that only happens on netlify and not my machine.

---

<div class="post-metadata">

**Author:** ![jtleniger](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jtleniger/32/321_2.png) [@jtleniger](https://answers.netlify.com/u/jtleniger)\
**Post date:** [April 13, 2019, 12:24am UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/5 "2019-04-13T00:24:50Z")

</div>

Note I’ve also tried wrapping the number in single quotes or a JSON.stringify with the same result.

---

<div class="post-metadata">

**Author:** ![DavidWells](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/davidwells/32/22_2.png) [@DavidWells](https://answers.netlify.com/u/DavidWells)\
**Post date:** [April 13, 2019, 12:30am UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/6 "2019-04-13T00:30:41Z")

</div>

@jtleniger can you try returning the data like:

```auto
callback(null, {
  statusCode: 200,
  body: JSON.stringify({
    result: result
  })
});

```

Let me know if that fixes the issue. It appears to be an encoding hiccup on our end with the raw number/string response

---

<div class="post-metadata">

**Author:** ![jtleniger](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jtleniger/32/321_2.png) [@jtleniger](https://answers.netlify.com/u/jtleniger)\
**Post date:** [April 13, 2019, 12:36am UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/7 "2019-04-13T00:36:25Z")

</div>

D’oh! That was the one permutation I didn’t try and that resolves the issue.

Thanks a bunch David! Loving this service.

---

<div class="post-metadata">

**Author:** ![DavidWells](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/davidwells/32/22_2.png) [@DavidWells](https://answers.netlify.com/u/DavidWells)\
**Post date:** [April 13, 2019, 12:39am UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/8 "2019-04-13T00:39:39Z")

</div>

Nice! Glad its fixed.

For a bunch more on functions check out [https://functions.netlify.com/](https://functions.netlify.com/) and this open source workshop on all things functions [GitHub - DavidWells/netlify-functions-workshop: Netlify Serverless Functions Workshop](https://github.com/DavidWells/netlify-functions-workshop#core-concepts)

---

<div class="post-metadata">

**Author:** ![jessepinho](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jessepinho/32/603_2.png) [@jessepinho](https://answers.netlify.com/u/jessepinho)\
**Post date:** [May 1, 2019, 4:33pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/9 "2019-05-01T16:33:40Z")

</div>

@DavidWells I’m experiencing the same thing. In my case, I’m proxying a request to an image service that requires authentication. So, my lambda function passes on the image request and attaches additional authentication headers, then proxies the image response directly back to the browser.

This works fine on localhost using `netlify-lambda`, but I’m getting the exact same error when using Netlify: `error decoding lambda response: json: cannot unmarshal object into Go struct field lambdaInvokeResponse.body of type string`

I suppose I can convert the image to base64 and deliver it that way, but that’s obviously not at all ideal. Can this please be fixed?

---

<div class="post-metadata">

**Author:** ![DavidWells](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/davidwells/32/22_2.png) [@DavidWells](https://answers.netlify.com/u/DavidWells)\
**Post date:** [May 1, 2019, 5:07pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/10 "2019-05-01T17:07:03Z")

</div>

Hey @jessepinho can you share the code with me?

I’d love to deploy a reproducible example of what you are working with (so I can run this up the flag pole)

---

<div class="post-metadata">

**Author:** ![jessepinho](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jessepinho/32/603_2.png) [@jessepinho](https://answers.netlify.com/u/jessepinho)\
**Post date:** [May 1, 2019, 5:29pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/11 "2019-05-01T17:29:19Z")

</div>

Sure! I’m stripping out most of the code for privacy reasons, but here’s the basic structure:

```auto
const fetch = require('isomorphic-fetch')

exports.handler = (event, context, callback) => {
  const { fileURL} = event.queryStringParameters
  const headers = { Authorization: `Bearer ${process.env.AUTH_TOKEN}` }
  fetch(fileURL, { headers })
    .then(result => result.buffer())
    .then(body => callback(null, { statusCode: 200, body }))
}

```

Note that the lambda should be invoked with a `fileURL` query parameter containing the URL to any image file — could just be a JPG you have sitting on a server somewhere.

---

<div class="post-metadata">

**Author:** ![DavidWells](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/davidwells/32/22_2.png) [@DavidWells](https://answers.netlify.com/u/DavidWells)\
**Post date:** [May 1, 2019, 6:41pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/12 "2019-05-01T18:41:49Z")

</div>

Hey @jessepinho

I updated the code and it appears to be working with these changes:

```auto
const fetch = require('isomorphic-fetch')

exports.handler = (event, context, callback) => {

  const { fileURL } = event.queryStringParameters

  const headers = { Authorization: `Bearer ${process.env.AUTH_TOKEN}` }

  fetch(fileURL, { headers })
    .then(result => result.buffer())
    .then(body => callback(null, { 
      statusCode: 200, 
      headers: {
        "Content-type": "image/jpeg"
      }, 
      body: body.toString('base64'),
      isBase64Encoded: true
    }))
}

```

I think\* you have to base64 encode the image for it to be returned probably by the lambda function.

Live example: [https://modest-sammet-0718e0.netlify.com/.netlify/functions/hello-world?fileURL=https://www.fillmurray.com/460/300](https://modest-sammet-0718e0.netlify.com/.netlify/functions/hello-world?fileURL=https://www.fillmurray.com/460/300)

---

<div class="post-metadata">

**Author:** ![jessepinho](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jessepinho/32/603_2.png) [@jessepinho](https://answers.netlify.com/u/jessepinho)\
**Post date:** [May 1, 2019, 7:02pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/13 "2019-05-01T19:02:18Z")

</div>

@DavidWells awesome, thank you! This did the trick.

However, it’s pretty slow at the moment. I’m guessing that’s due to the base64 encoding and decoding that has to happen on the server side. If there were a way to avoid doing any base64 encoding, that would be ideal — just a little feature request. Thanks!

---

<div class="post-metadata">

**Author:** ![DavidWells](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/davidwells/32/22_2.png) [@DavidWells](https://answers.netlify.com/u/DavidWells)\
**Post date:** [May 1, 2019, 7:15pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/14 "2019-05-01T19:15:15Z")

</div>

You might want to checkout role-based redirect rules

> **[Role-based access control with JWT](https://docs.netlify.com/visitor-access/role-based-access-control/)**
>
> Use JSON Web Tokens (JWT), roles, and redirect rules to set granular access to your site or to specific pages with role-based access control.

This would protect / serve raw files from the CDN layer instead of via a function

---

<div class="post-metadata">

**Author:** ![jessepinho](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jessepinho/32/603_2.png) [@jessepinho](https://answers.netlify.com/u/jessepinho)\
**Post date:** [May 1, 2019, 7:46pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/15 "2019-05-01T19:46:34Z")

</div>

Thanks, but I don’t think that would help — we need to proxy image requests to a different backend (Slack) that requires an auth token. So, it’s not images that we already have on our CDN 🙂

---

<div class="post-metadata">

**Author:** ![futuregerald](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/futuregerald/32/33_2.png) [@futuregerald](https://answers.netlify.com/u/futuregerald)\
**Post date:** [May 3, 2019, 2:10pm UTC](https://answers.netlify.com/t/debugging-a-function-returns-502/429/17 "2019-05-03T14:10:56Z")

</div>

@jessepinho, Using our structured redirects, you can actually add headers in your proxy requests to your backend, meaning you can include an `Authorization` header. More info [here](https://www.netlify.com/docs/redirects/#structured-configuration) and [here](https://www.netlify.com/blog/2017/10/17/introducing-structured-redirects-and-headers/).

At the moment our functions expect you to return a string, which is why David had to base64 encode it, but we have an open (internal) issue to change this behavior. We likely won’t get to it for a while, which is why we’re [hiring more platform developers](https://www.netlify.com/careers/).
