2

I'm developing a backend for an existing application which sends Gzipped data to an endpoint on Netlify Functions. However, I wasn't able to get the body as a Buffer instead of a string.

This means that Zlib is unable to decompress the body since invalid bytes have been replaced by the Unicode replacement character and they turn into EF BF BD. This is my code:

import { Handler } from "@netlify/functions";

export const handler: Handler = async (event, context) => {
  console.log(Buffer.from(event.body).toString("hex"));
  const extractedData = zlib.gunzipSync(event.body).toString();
} // The problem is that "event.body" is a string.

This is reproducible by using this command. It sends "Hello World!" compressed with Gzip:

base64 -d <<< "H4sIAAAAAAAA//NIzcnJVwjPL8pJUQQAoxwpHAwAAAA=" | curl -X POST --data-binary @- "http://localhost:8888/.netlify/functions/post"

However, my code doesn't produce the expected output because of the replacements:

Expected: 1f8b08000000000000fff348cdc9c95708cf2fca49510400a31c291c0c000000
Received: 1fefbfbd08000000000000efbfbdefbfbd48efbfbdefbfbdefbfbd5708efbfbd2fefbfbd49510400efbfbd1c291c0c000000

Is there any way to access the raw body of the request, preferably as a Buffer?

Gadget
  • 183
  • 1
  • 7
  • 1
    Netlify seems to say that it's officially not on their radar to offer raw body data. AWS Lambdas have a `rawbody` option in their config yml files. See this: https://answers.netlify.com/t/how-do-i-get-raw-body-in-my-lambda/5439/11 -- I tried this solution for verifying Stripe webhook requests and it works. You could possibly find a workaround along similar lines for your specific case. – anatolhiman Feb 17 '22 at 17:28

1 Answers1

1

After some research of how it's done in AWS, I found something who resolve my issue. The discord sign system need rawBody as stripe, that should do the same:

const rawBody = String(event.body).replace(/'/g, '\'').replace(/\\'/g,"'")

I found this with this by reading this comment: https://stackoverflow.com/a/71582521/5511370

And with the description in the doc of AWS of the function, I was able to do it in JS: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

scapes the characters in a string using JavaScript string rules.

Note
This function will turn any regular single quotes (') into escaped ones (\').
However, the escaped single quotes are not valid in JSON.
Thus, when the output from this function is used in a JSON property,
you must turn any escaped single quotes (\') back to regular single quotes (').
This is shown in the following example:

 $util.escapeJavaScript(data).replaceAll("\\'","'")
Martin Donadieu
  • 115
  • 1
  • 5