nicq
nicq

Reputation: 2314

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.

Upvotes: 9

Views: 14972

Answers (3)

Preetham Shreyas
Preetham Shreyas

Reputation: 37

Code example for managing binary data in AWS Gateway and Proxy+ Lambda. Or to upload and retrieve files from servers using AWS Gateway and Proxy Lambda+ Click here

Upvotes: -1

nicq
nicq

Reputation: 2314

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.

Upvotes: 12

Ka Hou Ieong
Ka Hou Ieong

Reputation: 6535

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'

Upvotes: 1

Related Questions