9

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.

nicq
  • 2,182
  • 5
  • 22
  • 38

3 Answers3

12

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.

nicq
  • 2,182
  • 5
  • 22
  • 38
  • 1
    hey, @nicq is it still not supported? Is it the best solution? – Lücks Aug 25 '20 at 17:27
  • @nicq Thanks mate. Your provided solution really helped and made my day. – Muhammad Arsalan Hassan Oct 03 '21 at 04:48
  • 1
    The Api Gateway does now support multipart/form-data, see https://stackoverflow.com/questions/60509051/aws-upload-csv-file-using-api-gateway-using-multipart-form-data as an example of the required changes. The main thing to keep in mind is to allow the gateway to accept binary media types. – Ralph Willgoss Jan 19 '22 at 17:01
1

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'
Ka Hou Ieong
  • 5,835
  • 3
  • 20
  • 21
-1

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