grosser
grosser

Reputation: 15097

Aws lambda messes up my encoding

I'm using

https://clifff.com/2015/10/01/2015-failed-experiments-with-aws-lambda/ + https://www.twilio.com/blog/2015/09/build-your-own-ivr-with-aws-lambda-amazon-api-gateway-and-twilio.html

to create an image resizing service on aws lambda ... I solved the content-type issue the first article was stuck at, but encoding seems like a dead end ... any help would be greatly appreciated!

ruby

Base64.decode64("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")

-> working image

GIF89a\u0001\u0000\u0001\u0000\x80\u0000\u0000\u0000\u0000\u0000\xFF\xFF\xFF!\xF9\u0004\u0001\u0000\u0000\u0000\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0001D\u0000;

api gateway with

$util.base64Decode("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")

-> broken image

GIF89a\u0001\u0000\u0001\u0000�\u0000\u0000\u0000\u0000\u0000���!�\u0004\u0001\u0000\u0000\u0000\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0001D\u0000;

Upvotes: 2

Views: 3380

Answers (2)

grepe
grepe

Reputation: 1977

OK, this is super old thread, but since the issue is still not resolved, and the binary support for API GW is miserably documented, I thought that somebody might find the workaround I found useful:

I believe, that binary data is passed around as UTF-8 strings somewhere inside the API GW. If you only need to return a tracking pixel (and not e.g. generated image), then you can avoid the problem with messed encoding by using an image that does not have problematic bytes in its binary data.

For example, the shortest tracking pixel (26 byte long GIF) has a byte with hexadecimal representation 0xFF in the middle. This will break the API GW. But if you edit this picture in hex editor and replace the byte with 0x00, you'll get something that is still a valid image (and EVEN Microsoft browsers don't complain about it), but can still be processed by API GW.

Just make your "Body Mapping" template look like this:

$util.base64Decode("R0lGODlhAQABAAAAACwAAAAAAQABAAACADs=")

Upvotes: 3

grosser
grosser

Reputation: 15097

Yeah ... looks like it's a know issue https://forums.aws.amazon.com/thread.jspa?messageID=668306&#668306

Upvotes: 2

Related Questions