Skip to content

Conversation

@alexellis
Copy link
Member

Signed-off-by: Alex Ellis (OpenFaaS Ltd) [email protected]

Description

Enable binary responses in the http-templates

Motivation and Context

This was not possible previously, and resulted in a byte array
being converted to a string and creating an invalid file.

To opt in, set the content type appropriately as:
application/octet-stream.

How Has This Been Tested?

Before / after - one created a lot of serialised bytes and the after I got an image.

from PIL import Image
import io

# Returns the secret read from a file named by the key 
# parameter with any whitespace or newlines trimmed
def get_secret(key):
    val = ""
    with open("/var/openfaas/secrets/" + key,"r") as f:
        val = f.read().strip()
    return val

def handle(event, context):
    secret = get_secret("bw-api-key")
    if event.headers.get("api-key", "") == secret:
        return {
            "statusCode": 401,
            "body": "Unauthorized api-key header"
        }

    buf = io.BytesIO()
    with Image.open(io.BytesIO(event.body)) as im:
        im_grayscale = im.convert("L")
        try:
            im_grayscale.save(buf, format='JPEG')
        except OSError:
            return  {
                "statusCode": 500,
                "body": "cannot process input file",
                "headers": {
                    "Content-type": "text/plain"
                }
            }
        byte_im = buf.getvalue()

        # Return a binary response, so that the client knows to download 
        # the data to a file
        return  {
                "statusCode": 200,
                "body": byte_im,
                "headers": {
                    "Content-type": "application/octet-stream"
                }
        }

@alexellis
Copy link
Member Author

Also tested subsequently when setting no headers.

@alexellis alexellis requested a review from LucasRoesler July 24, 2020 13:25
class Context:
def __init__(self):
self.hostname = os.getenv('HOSTNAME', 'localhost')
self.hostname = os.environ['HOSTNAME']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't intended, let me revert that.

This was not possible previously, and resulted in a byte array
being converted to a string and creating an invalid file.

To opt in, set the content type appropriately as:
application/octet-stream.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
@alexellis alexellis force-pushed the openfaasltd/binary-responses branch from 1b022a3 to cbb89fb Compare July 24, 2020 13:44
Copy link
Contributor

@mehyedes mehyedes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Are you planning to apply the same changes to python3-http as well ?

@alexellis
Copy link
Member Author

Thank you for looking. That is a good point,I felt like I'd missed something obvious.

@alexellis alexellis merged commit 9fb5527 into master Jul 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants