是否可以将具有 Content-Type:multipart/form-data 的请求发送到API网关?
Is it possible to send request with: Content-Type: multipart/form-data to API Gateway?
对于我来说,我尝试通过邮递员发送如下形式的数据:
In my case, I try to send form-data like below via Postman:
user[email]:extest829@ex user[password]:password user[password_confirmation]:password user[username]:testUser但是看来API Gateway丢失了内容.
But It seems that API Gateway loses the content.
当我将其发送为 application/x-www-form-urlencoded 或 application/json 时,一切正常.
Everything works fine when I send it as: application/x-www-form-urlencoded or application/json.
推荐答案AWS API网关不完全支持使用 mulipart/form-data ,尤其是当我们尝试通过mulipart/form发送文件时-数据.
Using mulipart/form-data is not fully supported by AWS API Gateway, especially when we try to send file via mulipart/form-data.
要与窗体中的其他数据一起发送图像,可能最好的解决方案是将其作为JSON发送,其中图像使用base64编码.
To send image along with other data from form, probably the best solution would be send it as JSON, where image is encoded with base64.
例如,当有人要发送时:
For example, when someone want to send:
头像应使用base64编码.它以文字形式显示图像,例如:
Avatar should be encoded with base64. It gives an image as a text, like:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyA...POST消息的内容为:
The content of POST message would be:
{ "user_account": { "avatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyA...", "username": "my name" } }API网关
在API网关的API下,打开模型并创建新模型,例如 UserAccount :
In API Gateway, under your API, open Models and create new model, for example UserAccount:
{ "$schema": "json-schema/draft-04/schema#", "title": "UserAccountUpdate", "type": "object", "properties": { "user": { "type": "object", "properties": { "avatar": { "type": "string" }, "username": { "type": "string" } } } } }在方法请求-> HTTP请求标头中设置:Content-Type.
在方法请求-> 请求正文中设置:application/json,并在模型使用时创建了 UserAccount 模型.
In Method Request -> Request Body set: application/json and as model use created UserAccount model.
在集成请求-> 内容处理中设置为:传递.
In Integration Request -> Content Handling set as: Passthrough.
在 Integration Request (集成请求)-> Body Mapping Templates (正文映射模板)中,选择:当没有模板与要求的Content-Type标头匹配时. (您还可以在此处使用其他两个选项,并设置其他映射模板.)
In Integration Request -> Body Mapping Templates choose: When no template matches the reuqest Content-Type header. (You can also use two other options here and set additional mapping templates).
后端
后端接收图像作为以base64编码的文本.因此,可能在进一步使用它之前,需要将其从文本解码为图像.
Backend receives an image as a text encoded with base64. So probably before it uses futher, it needs to decode it from text to image.
使用base64编码/解码图像非常流行,因此您应该在框架中找到一些合适的工具/库.
Encoding/decoding images with base64 is quite popular, so you should find some appropriate tools / libs in your frameworks.
更多推荐
AWS API Gateway:表单数据支持
发布评论