ASP.NET Core 3.1 get method body breaks the request

Issue happened on ASP.NET Core 3.1 Web API controller. I have a method in controller with [HttpGet] attribute and [FromBody] for only parameter:

[HttpGet]
public async Task<ActionResult> GetMessages([FromBody] MessageRM messagesRequest)

This method expects to receive a request model:
public class MessageRM
{
    public int RegionID { get; set; }
    public int? LastMessageID { get; set; }
    public int? StartMessageID { get; set; }
    public int? PageSize { get; set; }
}

When I do make a get request to this method without body the returning result is:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"|f6d2c92d-47865e9e3ad9a87d."}

That was quite unexpected. Had a feeling that I’m missing something important, this feeling increased when I passed the body. At first with ‘{}’, then fully simulating my request object. Either of these requests contained ‘application/json’ content-type header. Both times same response:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|f6d2c92e-47865e9e3ad9a87d.","errors":{"$":["The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]}}

Any suggestions?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Change [HttpGet] for [HttpPost] and issue a POST request instead of a GET.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x