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?