Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in Postman request – deserialization

This is in the POSTMAN request body:

{
    "roles":  
        [
            {
                "role_id": "TEST1",
                "role_name": "Для тестов"
            },
            {
                "role_id": "TEST3",
                "role_name": "Для тестов
            }
        ]
}

Model as it is :

public class Role 
{
    [JsonProperty(PropertyName = "role_id")]
    public string role_id { get; set; }


    [JsonProperty(PropertyName = "role_name")]
    public string role_name { get; set; }

    
}

Controller api-method – an Error appeared in Request

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

HttpPost("[action]/")]
public ActionResult<UserReply> SetRoleMessage(string message_text, List<Role> roles)
{
               //CODE
}

HERE is Postman results with an Error :
enter image description here

>Solution :

You send in an object with a property "roles" which is an array. But what your controller expects is that you send in an array.

Update your body to

[
    {
        "role_id": "TEST1",
        "role_name": "Для тестов"
    },
    {
        "role_id": "TEST3",
        "role_name": "Для тестов"
    }
]

As a side note, I would also use [FromQuery] and [FromBody] in your controller method to make more clear what is what:

public ActionResult<UserReply> SetRoleMessage([FromQuery] string message_text, [FromBody] List<Role> roles)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading