API网关-发布multipart \ form-data [英] API Gateway - Post multipart\form-data

查看:185
本文介绍了API网关-发布multipart \ form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我的问题可能与此有点类似..

Seems my question maybe a little similar to this one.

我的API网关中有一个API,并且正在通过HTTP代理访问POST的multipart/form-data文件的端点.

I have an API within my API Gateway and am doing a HTTP proxy through to an endpoint that POST's multipart/form-data files.

如果我直接调用HTTP终结点(而不是通过API网关)-使用邮递员,它会按预期工作,但是,使用API​​网关终结点(通过邮递员)会失败.

If I call the HTTP endpoint directly (not through the API gateway) - using postman, it works as expected, however, using the API gateway endpoint (through postman) fails.

我已经比较了两个请求(通过提琴手和CloudWatch日志),它们看起来是相同的:

I have compared both requests (through fiddler and CloudWatch logs) which seem to be identical:

请求直接API调用(有效):

Request for direct API call (working):

POST https://domainname/api/v1/documents HTTP/1.1
Host: api.service
Connection: keep-alive
Content-Length: 202
Authorization: AuthToken
Postman-Token: a75869d6-1d64-6b9f-513d-a80ac192c8e1
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
docMetaInfo: some extra data needed
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryB85rsPlMffA2fziS
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

------WebKitFormBoundaryB85rsPlMffA2fziS
Content-Disposition: form-data; name=""; filename="Test.txt"
Content-Type: text/plain

This is a test Text File
------WebKitFormBoundaryB85rsPlMffA2fziS--

来自API网关的请求(不起作用):

Request from the API Gateway (not working):

POST https://GATEWAY_domainname/api/v1/documents HTTP/1.1
Host: api-Gateway.service
Connection: keep-alive
Content-Length: 202
Authorization: AuthToken
Postman-Token: e25536fa-3dfa-ddcb-8ca6-3f3552d2bc40
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
docMetaInfo: some extra data needed
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarybX9MyWBsuLGm6QIC

x-api-key: *********************
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

------WebKitFormBoundarybX9MyWBsuLGm6QIC
Content-Disposition: form-data; name=""; filename="Test.txt"
Content-Type: text/plain

This is a test Text File
------WebKitFormBoundarybX9MyWBsuLGm6QIC--

我已经从网关方面尝试了一些方法,包括更改Integration Request来为相同的内容类型映射一个新主体,但是没有运气.

I have tried a few things from the gateway side, including changing the Integration Request to map a new body for the same content-type, no luck.

据我所知,我只需要passthrough此调用,因此为什么它变得有点混乱-不需要数据操作/拦截?

As far as I am aware, I should only need to passthrough this call, hence why it's becoming a little confusing - there should be no need for data manipulation / interception?

我得到的错误是400-错误的请求(抱怨未找到file),但是正如您在请求中看到的那样,它在那里.

The error I get is 400 - bad request (complaining about the file not being found), but as you can see in the request, it's there.

有什么想法吗?

编辑 来自CloudWatch的日志位于同一APIGateway POST

EDIT Logs from CloudWatch on the same APIGateway POST

错误仍然是400-找不到文件

Error is still 400 - no file found

推荐答案

API网关当前不支持多部分表单数据.正在考虑将其用于将来的开发.同时,您将需要修改客户端以使用多个请求或单个组成部分的请求.

API Gateway does not currently support multipart form data. This is being considered for future development. In the meantime, you will need to modify your client to use multiple requests or a single one-part request.

更新:API网关现在支持二进制有效负载.只需将multipart/form-data定义为API的二进制媒体类型,然后将有效负载直接代理到Lambda函数.从那里,您可以解析正文以获取文件内容.应该有可用的库来帮助解析多部分主体(Node.js中的 parse-multipart 例如).

Update: API Gateway now supports binary payloads. Simply define multipart/form-data as a binary media type for your API and proxy the payload directly to a Lambda function. From there you can parse the body to get your file content. There should be libraries available to help parse the multipart body (parse-multipart in Node.js for example).

这篇关于API网关-发布multipart \ form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆