question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

why return base64 encoded?

See original GitHub issue

Hi,

If I understand correctly from #64 and #66 that listing ‘application/json’ in the binaryMimeTypes is for gzip support.

I tried to mimic the example with a simpler version. However I got the base64 encoded return. Below is the setting for lambda.js and app.js

// lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')

// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely
// due to a compressed response (e.g. gzip) which has not been handled correctly
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to
// binaryMimeTypes below, then redeploy (`npm run package-deploy`)
const binaryMimeTypes = [
  'application/javascript',
  'application/json',
  'application/octet-stream',
  'application/xml',
  'font/eot',
  'font/opentype',
  'font/otf',
  'image/jpeg',
  'image/png',
  'image/svg+xml',
  'text/comma-separated-values',
  'text/css',
  'text/html',
  'text/javascript',
  'text/plain',
  'text/text',
  'text/xml'
]
const server = awsServerlessExpress.createServer(app, null, binaryMimeTypes)

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)
// app.js
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const compression = require('compression')
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const app = express()


app.use(compression())
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(awsServerlessExpressMiddleware.eventContext())

app.get('/testing', function (req, res) {
  res.json({testing: 'testing'})
})

module.exports = app

I got return value of eyJ0ZXN0aW5nIjoidGVzdGluZyJ9 instead of {testing: ‘testing’}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:11

github_iconTop GitHub Comments

21reactions
goldenbearkincommented, Sep 26, 2017

For those who’s struggling the useage of binaryMimeTypes, here is my sharing. Please correct me if anything wrong.

The whole thing of adding MIME types to binaryMimeTypes is for compression. e.g. you want to gzip the application/json payload. let’s first quote from @brettstack :

“Express response -> compression/gzip middleware (if you use it) -> aws-serverless-express (encodes to base64 if content-type is specified in binaryMimeTypes in Lambda handler) -> API Gateway (decodes base64 if content-type matches a binary mime type specified on API) -> Client”

here is my little supplement for his quote.

  1. Lambda doesn’t support binary type, so aws-serverlesss-express needs to encode the compression to base64
  2. API gateway doesn’t support gzip, but support binary type, so what it does is to let the express middleware do the compression, and decodes the base64 encoded payload received from lambda and finally pass to client.
6reactions
sudoseancommented, Jan 9, 2019

I am working behind some gateway that is external to aws. Is there a way to turn of the base64 encoding?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does base64 encode return bytes instead of string directly?
You are correct that base64 is meant to be a textual representation of binary data. However, you are neglecting constraints on the actual ......
Read more >
Base64 - MDN Web Docs Glossary: Definitions of ... - Mozilla
Encoded size increase​​ This means that the Base64 version of a string or file will be at least 133% the size of its...
Read more >
why return base64 encoded? - vendia/serverless-express
Hi, If I understand correctly from #64 and #66 that listing 'application/json' in the binaryMimeTypes is for gzip support.
Read more >
Encoding and Decoding Base64 Strings in Python - Stack Abuse
Base64 encoding allows us to convert bytes containing binary or text data to ASCII characters.
Read more >
base64_encode - Manual - PHP
Encodes the given string with base64. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found