-1

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?

Heretic Monkey
  • 10,498
  • 6
  • 45
  • 102
hitpoint
  • 55
  • 5

1 Answers1

1

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

Ricardo Peres
  • 11,795
  • 4
  • 45
  • 64
  • Yeah, already tested that, works fine. But i'm still curious for the cause. Can you add more details. Is this a bypass for an error or there is a spec for this somewhere. Some link to msdn where is explicitly written: "GET IS NOT ALLOWED TO HAVE A BODY"? – hitpoint Aug 27 '20 at 21:34
  • 3
    @hitpoint As the linked question's answers note, the RFC does not specifically forbid the use of a body in a GET request, but does not give any semantic importance to it either, which means most implementers of server-side frameworks ignore the body of GET requests. – Heretic Monkey Aug 27 '20 at 21:39