Skip to content
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

Getting binary support working in blueprint ASP.NET Core Web App #240

Closed
mbp opened this issue Mar 8, 2018 · 5 comments
Closed

Getting binary support working in blueprint ASP.NET Core Web App #240

mbp opened this issue Mar 8, 2018 · 5 comments
Labels
guidance Question that needs advice or information.

Comments

@mbp
Copy link

mbp commented Mar 8, 2018

I am having some problems getting binary support working with the ASP.NET Core 2 integration.

So I tried with the "ASP.NET Core Web App" blueprint, and it actually contains a favicon.ico file, which is not displayed when published.

I think the blueprint should work out of the box in the same way as running on your own machine. Or maybe you should just remove the favicon.ico.

I read the instructions with regards to Binary Response Content here: https://github.com/aws/aws-lambda-dotnet/blob/master/Libraries/src/Amazon.Lambda.AspNetCoreServer/README.md

So I added the following line to LambdaEntryPoint.Init(): RegisterResponseContentEncodingForContentType("image/x-icon", ResponseContentEncoding.Base64);

And I added the Binary Media Type in console:
image

Lambda log correctly shows that response is base64 encoded:

START RequestId: 11a7e691-22d3-11e8-9b13-ef274abd9c03 Version: $LATEST
Incoming GET requests to /favicon.ico
ASP.NET Core Request PathBase: /Prod, Path: /favicon.ico
[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting GET https://XXX.execute-api.eu-west-1.amazonaws.com/Prod/favicon.ico 
[Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/favicon.ico'. Physical path: '/var/task/wwwroot/favicon.ico' 
[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.5675ms 200 image/x-icon 
Response Base 64 Encoded: True
END RequestId: 11a7e691-22d3-11e8-9b13-ef274abd9c03

API Gateway log:

Starting execution for request: 577e5bb3-22d4-11e8-b061-1775958b34a4
HTTP Method: GET, Resource Path: /favicon.ico
Successfully completed execution
Method completed with status: 200

What part am I missing?

I found quite many posts with people not making binary work with the lambda proxy integration. So I suspect there is a general issue.

@normj normj added the Question label Mar 13, 2018
@mbp
Copy link
Author

mbp commented Mar 15, 2018

I resolved this issue when talking to AWS Support.

The solution:
After changing "Binary Media Types", you must go and click "Deploy API":

image

@mbp mbp closed this as completed Mar 15, 2018
@cjnoname
Copy link

Hey bro, I followed your steps but still could not get the image/icon.

Did you do anything more than what you said?

Thanks!

@mbp
Copy link
Author

mbp commented Mar 20, 2018

Yeah, actually you need to set BinaryMediaTypes to */* when trying to show the image in the browser.

BinaryMediaType image/x-icon will only work if client sends Accept header. Here is a way to test:

$ curl -sH "Accept: image/x-icon" https://XXX.execute-api.eu-west-1.amazonaws.com/Prod/favicon.ico -o /tmp/favicon.ico
$ file /tmp/favicon.ico
/tmp/favicon.ico: MS Windows icon resource - 3 icons, 16x16, 256-colors

@cjnoname
Copy link

cjnoname commented Mar 20, 2018

ignore it :)

@badcommandorfilename
Copy link

badcommandorfilename commented Apr 20, 2018

I'm just going to leave this here so it hopefully saves someone else an hour...

The default "Accept" header in the request for .png files is "image/apng" not "image/png"! Everything worked once I added:

        protected override void Init(IWebHostBuilder builder)
        {
            //NB: Serverless WebAPI needs to have special config to serve binary file types: 
            RegisterResponseContentEncodingForContentType("image/png", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/jpeg", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/gif", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/apng", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/webp", ResponseContentEncoding.Base64);
            RegisterResponseContentEncodingForContentType("image/x-icon", ResponseContentEncoding.Base64);

            builder
                .UseStartup<Startup>();
        }

Edit: Accept headers for Chrome and IE11 seem to use image/apng as default but Firefox sends */* for <img> tags! You can add */* to the above list, and it seems to work but YMMV.

@diehlaws diehlaws added guidance Question that needs advice or information. and removed Question labels Jan 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

5 participants