Swagger UI sends multiple headers bound to a single action parameter as a single json object, rather than as multiple headers

Roddi Walker

I enabled Swagger in my asp.net core 3.1 API by following the usual MS docs. https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio It works fine.

The following controller works great in Postman. myBody has its Body1 and Body2 fields bound from the POST request's json body. myHeader has its Header1 and Header2 bound from the request's two "Header1" and "Header2" headers.

namespace MyApi.Controllers
{
    [ApiController]
    [Route("test")]
    public class TestController : ControllerBase
    {
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult Post(
            [FromHeader] MyHeaders myHeaders,
            MyBody myBody)
        {
            return Ok();
        }
    }

    public class MyHeaders
    {
        [FromHeader]
        public string Header1 { get; set; }

        [FromHeader]
        public string Header2 { get; set; }
    }

    public class MyBody
    {
        public string Body1 { get; set; }

        public string Body2 { get; set; }
    }
}

However Swagger UI only passes 1 json object for the two headers: [swagger ui][1]

And it generates a corresponding curl command: curl -X POST "https://localhost:5001/test" -H "accept: */*" -H "myHeaders: header1,string,header2,string" -H "Content-Type: application/json" -d "{\"body1\":\"string\",\"body2\":\"string\"}"

The problem is the -H "myHeaders: header1,string,header2,string" portion. Model binding sets myHeaders.Header1 and .Header2 to null, as expected, because the header is named "myHeaders". Replacing the portion with **-H "Header1: cat" -H "Header2: dog" works correctly.

Asp.net is clever enough to map separate headers into a single action parameter by matching their names. But how can I get Swagger have multiple headers the UI, so its curl command works? I know I could replace [FromHeader] MyHeaders myHeaders with [FromHeader] string Header1, [FromHeader] string Header2, but I want to avoid that. There will be dozens of actions that all receive the same set of headers.

Roddi Walker

Solution is given in Yura's answer in How to use [FromHeader] attribute with custom model binding in Asp.Net Core 2.2 It is to change [FromHeader] MyHeaders myHeaders to [FromQuery] MyHeaders myHeaders. This gets Swagger UI working, even though the FromQuery seems to be contradicted by the FromHeader attribute on Header1 and Header2.

The fixed code is:

public class TestController : ControllerBase
{
    [HttpPost]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public ActionResult Post(
        [FromQuery] MyHeaders myHeaders, // FromHeader is changed to FromQuery
        MyBody myBody)
    {
        return Ok();
    }
}

public class MyHeaders // unchanged
{
    [FromHeader]
    public string Header1 { get; set; }

    [FromHeader]
    public string Header2 { get; set; }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SSRS Multiple Headers-Footers /Single List

From Dev

xmlstarlet XPath expression selects single result rather than multiple

From Dev

xmlstarlet XPath expression selects single result rather than multiple

From Dev

AngularJS directive calls multiple time rather than to call a single time

From Dev

Pandas pivot/merge multiple columns into single, using column headers as values

From Dev

Are multiple status code headers allowed in a single HTTP response?

From Dev

multiple columns in a single parameter

From Dev

multiple columns in a single parameter

From Dev

How to combine multiple arrays into single JSON object

From Dev

Saving multiple Json object in single file

From Dev

Gson: Mapping multiple json objects to a single object

From Dev

Multiple Mutations on a single object

From Dev

Jquery Ajax method working with multiple json object but not with single json object

From Dev

Pass multiple values in single parameter

From Dev

Passing multiple values in single parameter

From Dev

How to merge multiple json objects into a single json object using python

From Dev

Mapping of multiple @queryParam into a single object

From Dev

Creating multiple objects to a single object

From Dev

Define all headers as a single dictionary

From Dev

Multiple values on HTTP headers

From Dev

HTTP - Multiple Trailer Headers

From Dev

Multiple arrays not outputting headers

From Dev

Read CSV with multiple Headers

From Dev

Posting Multiple Headers with Flurl

From Dev

Read CSV with multiple Headers

From Dev

Multiple Headers and ARIA Roles

From Dev

Using terminaltables, how can I get all my data in a single table, rather than split across multiple tables?

From Dev

Multiple lists in single json output

From Dev

jQuery.ajax() sends request more than once if headers object is set

Related Related

  1. 1

    SSRS Multiple Headers-Footers /Single List

  2. 2

    xmlstarlet XPath expression selects single result rather than multiple

  3. 3

    xmlstarlet XPath expression selects single result rather than multiple

  4. 4

    AngularJS directive calls multiple time rather than to call a single time

  5. 5

    Pandas pivot/merge multiple columns into single, using column headers as values

  6. 6

    Are multiple status code headers allowed in a single HTTP response?

  7. 7

    multiple columns in a single parameter

  8. 8

    multiple columns in a single parameter

  9. 9

    How to combine multiple arrays into single JSON object

  10. 10

    Saving multiple Json object in single file

  11. 11

    Gson: Mapping multiple json objects to a single object

  12. 12

    Multiple Mutations on a single object

  13. 13

    Jquery Ajax method working with multiple json object but not with single json object

  14. 14

    Pass multiple values in single parameter

  15. 15

    Passing multiple values in single parameter

  16. 16

    How to merge multiple json objects into a single json object using python

  17. 17

    Mapping of multiple @queryParam into a single object

  18. 18

    Creating multiple objects to a single object

  19. 19

    Define all headers as a single dictionary

  20. 20

    Multiple values on HTTP headers

  21. 21

    HTTP - Multiple Trailer Headers

  22. 22

    Multiple arrays not outputting headers

  23. 23

    Read CSV with multiple Headers

  24. 24

    Posting Multiple Headers with Flurl

  25. 25

    Read CSV with multiple Headers

  26. 26

    Multiple Headers and ARIA Roles

  27. 27

    Using terminaltables, how can I get all my data in a single table, rather than split across multiple tables?

  28. 28

    Multiple lists in single json output

  29. 29

    jQuery.ajax() sends request more than once if headers object is set

HotTag

Archive