【发布时间】:2020-04-17 09:22:21
【问题描述】:
发生了什么
使用 API Gateway 将二进制数据传递给 Lambda 时,会出现乱码。
HTTP 请求标头的 ContentType 设置为 multipart/formdata。
部分乱码二进制数据如下图。
# Collect
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 89 50 4e 47 0d 0a 1a 0a ┊ 00 00 00 0d 49 48 44 52 │×PNG__•_┊000_IHDR│
│00000010│ 00 00 02 00 00 00 02 00 ┊ 08 06 00 00 00 f4 78 d4 │00•000•0┊••000×x×│
│00000020│ fa 00 00 00 01 73 52 47 ┊ 42 00 ae ce 1c e9 00 00 │×000•sRG┊B0×ו×00│
│00000030│ 00 ea 65 58 49 66 4d 4d ┊ 00 2a 00 00 00 08 00 06 │0×eXIfMM┊0*000•0•│
│00000040│ 01 12 00 03 00 00 00 01 ┊ 00 01 00 00 01 1a 00 05 │••0•000•┊0•00••0•│
│00000050│ 00 00 00 01 00 00 00 56 ┊ 01 1b 00 05 00 00 00 01 │000•000V┊••0•000•│
│00000060│ 00 00 00 5e 01 31 00 02 ┊ 00 00 00 26 00 00 00 66 │000^•10•┊000&000f│
│00000070│ 01 32 00 02 00 00 00 14 ┊ 00 00 00 8c 87 69 00 04 │•20•000•┊000××i0•│
│00000080│ 00 00 00 01 00 00 00 a0 ┊ 00 00 00 00 00 00 00 48 │000•000×┊0000000H│
# Garbled
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ ef bf bd 50 4e 47 0d 0a ┊ 1a 0a 00 00 00 0d 49 48 │×××PNG__┊•_000_IH│
│00000010│ 44 52 00 00 02 00 00 00 ┊ 02 00 08 06 00 00 00 ef │DR00•000┊•0••000×│
│00000020│ bf bd 78 ef bf bd ef bf ┊ bd 00 00 00 01 73 52 47 │××x×××××┊×000•sRG│
│00000030│ 42 00 ef bf bd ef bf bd ┊ 1c ef bf bd 00 00 00 ef │B0××××××┊•×××000×│
│00000040│ bf bd 65 58 49 66 4d 4d ┊ 00 2a 00 00 00 08 00 06 │××eXIfMM┊0*000•0•│
│00000050│ 01 12 00 03 00 00 00 01 ┊ 00 01 00 00 01 1a 00 05 │••0•000•┊0•00••0•│
│00000060│ 00 00 00 01 00 00 00 56 ┊ 01 1b 00 05 00 00 00 01 │000•000V┊••0•000•│
│00000070│ 00 00 00 5e 01 31 00 02 ┊ 00 00 00 26 00 00 00 66 │000^•10•┊000&000f│
│00000080│ 01 32 00 02 00 00 00 14 ┊ 00 00 00 ef bf bd ef bf │•20•000•┊000×××××│
我应该如何解决它?
环境
curl 向端点发送 HTTP 请求。
使用了以下命令。curl -H "Content-Type: multipart/form-data" -F file="@my_file.png" -X POST <url to the endpoint>
HTTP 请求正文从 API 网关传递给 Lambda。
API Gateway 上的二进制媒体类型设置为multipart/form data 和image/png。
给 Lambda 函数一个以 Base64 编码的主体,因此它会在 Node.js 上对其进行解码。
【问题讨论】:
标签: curl aws-lambda aws-api-gateway multipartform-data binary-data