Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Api Gateway as a HTTP Proxy is currupting binary uploaded image files

I have a ruby on rails app that takes an image file, "attaches it to a member", and uploads it to s3. When I use insomnia and POST directly to the app ... it works, however when I use the exact same endpoint behind AWS Api Gateway, the image is corrupt and not viewable.


Here is the comparison of the requests.

  • LEFT = directly posted to the rails app
  • RIGHT = through api gateway

https://www.diffchecker.com/wwUmpB5W

Something I noticed, is that the paperclip gem is running different commands. It's evident that paperclip realized that the file is not an image when being passed through API gateway.


Here are some potentially relevant screenshots

method execution integration request api gateway settings


Here is the rails code:

def create
  logger.info 'upload_attachment_api_general_v1'
  logger.info params
  logger.info request.env
  @file = current_merchant.members.find(params[:member_id]).attachments.new(file: params[:file], label: params[:label])
  if params[:file] && @file.save
    render json: @file
  else
    render json: @file.errors, status: :unprocessable_entity
  end
end
like image 827
Andrew Wei Avatar asked Oct 09 '18 16:10

Andrew Wei


People also ask

What is HTTP proxy in API gateway?

An HTTP proxy integration enables you to connect an API route to a publicly routable HTTP endpoint. With this integration type, API Gateway passes the entire request and response between the frontend and the backend. To create an HTTP proxy integration, provide the URL of a publicly routable HTTP endpoint.

Does API gateway allow HTTP?

You can integrate an API method with an HTTP endpoint using the HTTP proxy integration or the HTTP custom integration. API Gateway supports the following endpoint ports: 80, 443 and 1024-65535.

How do you upload any type of binary file to S3 via API gateway?

To upload a binary file (image) to an S3 bucket using API Gateway, you must activate binary support for your API Gateway REST API. To allow your API to access your S3 bucket, you must also create an AWS Identity and Access Management (IAM) role.

Is AWS API gateway reverse proxy?

API gateway functions as a reverse proxy to accept all API calls, aggregate the required services, and return the right result. An API gateway has more functionalities than an API proxy, especially in security and monitoring. The Backend for Frontend (BFF) design is commonly used in Microservices development.


1 Answers

I figured it out. The content type is NOT an image/png ... the content type is multipart/form-data

multipart

like image 84
Andrew Wei Avatar answered Sep 28 '22 08:09

Andrew Wei