Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway: form-data support

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]:[email protected]
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.

like image 682
nicq Avatar asked Jan 09 '17 21:01

nicq


2 Answers

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.

like image 66
nicq Avatar answered Nov 10 '22 00:11

nicq


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'
like image 35
Ka Hou Ieong Avatar answered Nov 10 '22 00:11

Ka Hou Ieong