AWS API Gateway:表单数据支持

编程入门 行业动态 更新时间:2024-10-27 22:19:19
本文介绍了AWS API Gateway:表单数据支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以将具有 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:表单数据支持

    本文发布于:2023-11-15 09:24:44,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1593184.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:表单   数据   AWS   API   Gateway

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!