Skip to content

Enable binary responses in the http-templates #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2020

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
@@ -17,41 +17,48 @@ def __init__(self):

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.

2 participants