This repository was archived by the owner on Dec 9, 2024. It is now read-only.
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
Binary support for HTTP response #289
Open
Description
Current implementation support HTTP responses via special JSON object schema. Right now only string is supported as a response body. EG needs to support also binary responses. Requirements:
- no requirement for using any special function wrapper/handler
- works across languages
- toggle per invocation not per function (binary/nonbinary response can be changed between invocation without changing EG configuration)
Possible solution isBinary
flag and base64 encoding
Right now the supported object looks like this:
{
"statusCode": 200,
"headers": {
"content-type": "text/html"
},
"body": "<hello/>"
}
The proposition is to support additional flag isBinary
that will indicate that body
is base64 encoded string that carries a binary payload. EG will decode it and return a binary response to the requester. We already pass a Base64 string to a function in case of binary input data so this solution is "symmetrical".
{
"statusCode": 200,
"headers": {
"content-type": "text/html"
},
"body": "<base64 string>",
"isBinary": true
}
Node.js Example
exports.image = (event, context, callback) => {
fs.readFile("./image.jpg", { encoding: "base64" }, function(err, data) {
callback(null, {
statusCode: 200,
headers: {
"content-type": "image/jpeg"
},
isBinary: true,
body: data
});
});
});
Python Example
def main(event, context):
with open('./image.jpg', 'rb') as f:
body = f.read().encode('base64')
return {
"statusCode": 200,
"headers": {
"content-type": "image/jpeg"
},
"isBinary": True,
"body": body
}