AWS API Gateway: form-data support

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

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.com user[password]:password user[password_confirmation]:password user[username]:testUser 

But It seems that API Gateway loses the content.

Everything works fine when I send it as: application/x-www-form-urlencoded or application/json.

回答1:

Using mulipart/form-data is not fully supported by AWS API Gateway, especially when we try to send file via mulipart/form-data.

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:

  1. Username (string)
  2. Avatar (image)

Avatar should be encoded with base64. It gives an image as a text, like:

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

The content of POST message would be:

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

API Gateway

In API Gateway, under your API, open Models and create new model, for example UserAccount:

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

In Method Request -> HTTP Request Headers set: Content-Type.

In Method Request -> Request Body set: application/json and as model use created UserAccount model.

In Integration Request -> Content Handling set as: Passthrough.

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).

Backend

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.

Encoding/decoding images with base64 is quite popular, so you should find some appropriate tools / libs in your frameworks.



回答2:

When you are sending request with Content-Type: multipart/form-data to API Gateway, you need to pass through your original Content-Type header to your integration endpoint.

aws apigateway update-integration \      --rest-api-id a1b2c3d4e5 \      --resource-id a1b2c3 \      --http-method POST \      --patch-operations op='replace',path='/requestParameters/integration.request.header.Content-Type',value='method.request.header.Content-Type' 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!