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?
Hey @jtleniger
This looks like a possible encoding bug on our end.
What does your function return. Aka what is result
on line 149
const result = distance(geo1.lat, geo1.lng, geo2.lat, geo2.lng);
callback(null, {
statusCode: 200,
body: result
});
Thanks for the report!
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.
Note I’ve also tried wrapping the number in single quotes or a JSON.stringify with the same result.
@jtleniger can you try returning the data like:
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
1 Like
D’oh! That was the one permutation I didn’t try and that resolves the issue.
Thanks a bunch David! Loving this service.
1 Like
Nice! Glad its fixed.
For a bunch more on functions check out https://functions.netlify.com/ and this open source workshop on all things functions GitHub - DavidWells/netlify-functions-workshop: Netlify Serverless Functions Workshop
3 Likes
@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?
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)
1 Like
Sure! I’m stripping out most of the code for privacy reasons, but here’s the basic structure:
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.
Hey @jessepinho
I updated the code and it appears to be working with these changes:
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
@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!
You might want to checkout role-based redirect rules
This would protect / serve raw files from the CDN layer instead of via a function
1 Like
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
@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 and here.
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.
1 Like