refit icon indicating copy to clipboard operation
refit copied to clipboard

[BUG] Content-Type of Header causes a 415 error in WebAPI

Open bestpika opened this issue 3 years ago • 3 comments

Describe the bug

I have a WebAPI function as follows.

  • LoginController.cs
[HttpPost]
public Login Post([FromBody] string token)
{
  ...
}
  • BaseAPI.cs
[Post("/api/login")]
Task<Login> Login([Body] string token);

I found that "HTTP 415 Unsupported Media Type" keeps appearing when sending data. Then when I checked the sent data, I found that the "Content-Type" is "text/plain; charset=UTF-8". However, the WebAPI in dot NET 6 only accepts "application/json" data, so I tried to modify the header, but I found that no matter how I modify it, there is an extra paragraph "text/plain; charset=UTF-8" in front of it.

Steps To Reproduce

Expected behavior

Screenshots

Environment

  • Blazor .NET 6 PWA
  • Edge 99.0.1150.11 beta x64

Additional context

bestpika avatar Feb 11 '22 01:02 bestpika

I am experiencing this as well when trying to POST SOAP XML to an endpoint.

Resulting header: Content-Type: text/plain; charset=utf-8, application/soap+xml; charset=utf-8

Interface:

    [Headers("Content-type: application/soap+xml; charset=utf-8")]
    public interface ISOAPAPI
    {
        [Post("/service.asmx")]
        Task PostDocument([Body] string xmlDocument);
    }

This exists in 6.3.2 and 6.2.16. I will keep trying other versions.

rorpage avatar Feb 17 '22 21:02 rorpage

@bestpika I got mine to work by changing the Headers to be:

[Headers("Content-Type: application/soap+xml; charset=utf-8")]

Specifically, Content-Type with a capital C and T.

rorpage avatar Feb 17 '22 21:02 rorpage

This would be the kind of thing it would be handy to have a Roslyn Analyzer for...

james-s-tayler avatar May 28 '22 06:05 james-s-tayler