unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.46k stars 176 forks source link

How to set Content-Type #530

Closed YoshihiroIto closed 2 years ago

YoshihiroIto commented 3 years ago

I am trying to create an API that returns a JPEG binary using WebApiController.

As shown in the sample HttpContext.OpenResponseStream() I used this.

I checked the response header.

Content-Type: text/html; charset=utf-8

Then I set ContentType to "image/jpeg".

await using (var stream = HttpContext.OpenResponseStream(false, false))
{
    HttpContext.Response.ContentType = "image/jpeg";
    await stream.WriteAsync(image.AsMemory(0, image.Length)).ConfigureAwait(false);
}

I checked the response header again.

Content-Type: image/jpeg; charset=utf-8

I don't want ; charset=utf-8.

How do I set the Content-Type?

haeferer commented 3 years ago

Hi,

can you try to additional set encoding to null. Default is UTF-8 (which makes no sense for image)

HttpContext.Response.ContentType = "image/jpeg";
HttpContext.Response.ContentEncoding  = null;   
YoshihiroIto commented 3 years ago

Thanks for your reply. I tried your advice, but I was unable to resolve this issue.

I then proceeded to do some research around this issue and came up with some findings which I will share.

The problem I reported earlier happens when using HttpListenerMode.EmbedIO, it does not happen when using HttpListenerMode.Microsoft.

However, when using HttpListenerMode.Microsoft in the Xamarin Android environment, another problem occurs and it does not work.

The other problem is that routing with parameters does not seem to work. For example (from Sample)

[Route(HttpVerb.Get, "/peopleByName/{name}")]
pop1989bb commented 3 years ago

Hi YoshihiroIto,

I can't confirm, that Routing with parameters doesn't work.

[Route(verb: HttpVerbs.Get, "/site/mandator-key-exists/{key}")]
public Task<bool> MandatorKeyExists(string key)
{
       return Task<bool>.Factory.StartNew(() => Models.SiteModel.MandatorKeyExists(this.Application, key));
}

This is a working example from my rest service. The method receives the parameter key as expected.

YoshihiroIto commented 3 years ago

Hi pop1989bb,

My code is

[Route(HttpVerbs.Get, "/cover/{localPath}/{maxSize}")]
public async Task GetCoverImageAsync(string localPath, int maxSize)
{
    byte[] image = .....

    await using var stream = HttpContext.OpenResponseStream(false, false);
    await stream.WriteAsync(image.AsMemory(0, image.Length)).ConfigureAwait(false);
}

Here is a summary of how it works

.WithMode(HttpListenerMode.Microsoft) .WithMode(HttpListenerMode.EmbedIO)
Windows work work (include ContentType problem)
Android not work work (include ContentType problem)
haeferer commented 3 years ago

Oh, i'm currently only work with HttpListenerMode.Microsoft on Windows.

I'm out :)

pop1989bb commented 3 years ago

I will try to do some tests on my API and let you know.

CaiusJard commented 3 years ago

Seems related:

https://github.com/unosquare/embedio/issues/487

And

https://github.com/unosquare/embedio/issues/505

?

pop1989bb commented 3 years ago

Yes, looks like that's the description of the problem.

Thanks for the hint!