Last active
March 24, 2022 15:16
-
-
Save zsim0n/c3796783859580dcd47b to your computer and use it in GitHub Desktop.
AWS API Gateway UTF-8 (unicode) workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// inspiration | |
// http://timnew.me/blog/2013/01/30/pitfall-in-node-crypto-and-base64-encoding/ | |
// https://github.com/kevva/base64-regex | |
Base64Util = { | |
encode64: function(text) { | |
return new Buffer(text, 'utf8').toString('base64'); | |
}, | |
decode64: function(base64) { | |
return new Buffer(base64, 'base64').toString('utf8'); | |
}, | |
test: function(base64) { | |
var regex = '^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$'; | |
return new RegExp(regex).test(base64); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": "YWJjw6XDpsO4" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// event.json: 'abcåæø' <-> 'YWJjw6XDpsO4' | |
require('base64-util.js) | |
var Base64 = new Base64Util(); | |
exports.handler = function (event, context) { | |
var data = event.data; | |
if (Base64.test(event.data)) { | |
data = Base64.decode64(event.data); | |
} | |
// your code | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
// iOS Swift example for encoding to base64 | |
import UIKit | |
import Foundation | |
var s : NSString = "abcåæø" | |
let data = s.dataUsingEncoding(NSUTF8StringEncoding) | |
let s64 = data?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment