question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consumes and Produces attributes are ignored in ASP .NET Core Web API

See original GitHub issue

I’m using NSwag.AspNetCore v13.0.6 (latest version as of this writing), and NSwag seem to be ignoring the Consumes and Produces attributes.

Am I missing anything?

[HttpPost("/api/stuff")]
[Consumes("text/plain")]
[Produces("text/plain")]
public IActionResult PostStuff()
{
    return Ok();
}

image

Relates to #420

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomasBaechlerConticommented, Sep 17, 2021

I am still having this issue. Since this issue claims that everything is correct, I investigated further.

I have an operation that is supposed to consume application/octet-stream, and I have a matching input processor.

[HttpPost]
[Consumes("application/octet-stream")]
public string EndpointConsumesBinaryData([FromBody] byte[] data)
{
    return string.Empty;
}
internal sealed class BinaryInputFormatter : InputFormatter
{
    public BinaryInputFormatter()
    {
        SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/octet-stream"));
    }

    public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
    {
        using var mem = new MemoryStream();
        await context.HttpContext.Request.Body.CopyToAsync(mem);
        return InputFormatterResult.Success(mem.ToArray());
    }

    protected override bool CanReadType(Type type)
    {
        return type.IsAssignableFrom(typeof(byte[]));
    }
}

I registered this input processor and the ASP.NET Core API explorer lists the operation correctly. Even NSwag seems to pick up the Consumes attribute. I created a Document Processor and put a breakpoint in the Process method.

Looking at DocumentProcessorContext.Document.Operations[0].Operation, I see that the ActualConsumes property contains “application/octect-stream”, but the RequestBody.Content dictionary only contains an “application/json” content.

So, in conclusion, NSwag does ignore the consumes attribute and seems to always assume application/json.

1reaction
ptr1120commented, Nov 2, 2019

the same problem on my side, ConsumesAttribute completely ignored.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to get ASP.NET web api to consume an ...
1 Answer. I was able to resolve this situation by creating a custom "Consume" attribute using the MVC code as a template and...
Read more >
Format response data in ASP.NET Core Web API
Learn how to format response data in ASP.NET Core Web API. ... use a specified format, ignoring a client's request for a different...
Read more >
Create web APIs with ASP.NET Core
Attributes. The Microsoft.AspNetCore.Mvc namespace provides attributes that can be used to configure the behavior of web API controllers and ...
Read more >
Understanding Produces And Consumes Attribute In MVC 6
Produces and Consumes Attributes are newly introduced in MVC 6 (ASP.NET Core) to control the content negotiation.
Read more >
Untitled
Consumes and Produces attributes are ignored in ASP .NET Core Web API ... AspNetCore.Docs/index.md at main - GitHub WebConsumesAttribute 类 (Microsoft.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found