1

我正在为现有应用程序开发一个后端,该应用程序将 Gzipped 数据发送到 Netlify Functions 上的端点。但是,我无法将正文作为缓冲区而不是字符串。

这意味着 Zlib 无法解压缩正文,因为无效字节已被 Unicode 替换字符替换,它们变成EF BF BD. 这是我的代码:

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.

这可以通过使用此命令重现。它发送“Hello World!” 用 Gzip 压缩:

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

但是,由于替换,我的代码不会产生预期的输出:

Expected: 1f8b08000000000000fff348cdc9c95708cf2fca49510400a31c291c0c000000
Received: 1fefbfbd08000000000000efbfbdefbfbd48efbfbdefbfbdefbfbd5708efbfbd2fefbfbd49510400efbfbd1c291c0c000000

有什么方法可以访问请求的原始正文,最好是作为缓冲区?

4

0 回答 0