gpt4 book ai didi

amazon-web-services - AWS API网关: form-data support

转载 作者:行者123 更新时间:2023-12-02 11:24:34 25 4
gpt4 key购买 nike

是否可以将带有:Content-Type: multipart/form-data 的请求发送到 API Gateway?

就我而言,我尝试通过 Postman 发送如下所示的表单数据:

user[email]:extest829@ex.com
user[password]:password
user[password_confirmation]:password
user[username]:testUser

但是 API Gateway 似乎丢失了内容。

当我将其发送为:application/x-www-form-urlencodedapplication/json时,一切正常。

最佳答案

AWS API Gateway 不完全支持使用 mulipart/form-data,特别是当我们尝试通过 mulipart/form-data 发送文件时。

要将图像与表单中的其他数据一起发送,最好的解决方案可能是将其作为 JSON 发送,其中图像使用 base64 进行编码。

例如,当有人想要发送:

  1. 用户名(字符串)
  2. 头像(图片)

头像应该使用base64编码。它以文本形式提供图像,例如:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyA...

POST 消息的内容为:

{
"user_account": {
"avatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyA...",
"username": "my name"
}
}

API网关

在 API Gateway 的 API 下,打开模型并创建新模型,例如 UserAccount:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "UserAccountUpdate",
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"avatar": { "type": "string" },
"username": { "type": "string" }
}
}
}
}

方法请求 -> HTTP 请求 header 中设置:Content-Type

方法请求 -> 请求正文中设置:application/json并作为模型使用创建的UserAccount模型.

集成请求 -> 内容处理中设置为:直通。

集成请求 -> 正文映射模板中选择:当没有模板与 reuqest Content-Type header 匹配时。 (您还可以在此处使用其他两个选项并设置其他映射模板)。

后端

后端接收以 base64 编码的文本形式的图像。因此,在进一步使用之前,可能需要将其从文本解码为图像。

使用 Base64 编码/解码图像非常流行,因此您应该在框架中找到一些合适的工具/库。

关于amazon-web-services - AWS API网关: form-data support,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41557109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com