nic*_*icq 6 amazon-web-services aws-api-gateway
是否可以发送请求:Content-Type:multipart/form-data到API Gateway?
就我而言,我尝试通过邮递员发送如下表格数据:
user[email]:extest829@ex.com
user[password]:password
user[password_confirmation]:password
user[username]:testUser
Run Code Online (Sandbox Code Playgroud)
但似乎API Gateway失去了内容.
当我发送它时,一切正常:application/x-www-form-urlencoded或application/json.
AWS API Gateway不完全支持使用mulipart/form-data,尤其是当我们尝试通过mulipart/form-data发送文件时.
要从表单发送图像和其他数据,可能最好的解决方案是将其作为JSON发送,其中图像使用base64编码.
例如,当有人想要发送:
头像应该用base64编码.它将图像作为文本,如:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyA...
Run Code Online (Sandbox Code Playgroud)
POST消息的内容将是:
{
"user_account": {
"avatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyA...",
"username": "my name"
}
}
Run Code Online (Sandbox Code Playgroud)
API网关
在API网关中,在您的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" }
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在方法请求 - > HTTP请求标头集:Content-Type.
在方法请求 - > 请求主体集:application/json和模型使用创建UserAccount模型.
在Integration Request - > Content Handling中设置为:Passthrough.
在集成请求 - > 正文映射模板中,选择:当没有模板与reuqest Content-Type标头匹配时.(您还可以在此处使用其他两个选项并设置其他映射模板).
后端
后端接收图像作为用base64编码的文本.所以可能在它进一步使用之前,它需要从文本到图像解码它.
使用base64编码/解码图像非常流行,因此您应该在框架中找到一些合适的工具/库.
| 归档时间: |
|
| 查看次数: |
7651 次 |
| 最近记录: |