You can capture the raw Request.Body and read the raw buffer out of that which is pretty straight forward. One way or another you need to do some custom processing of the Request.Body to get the raw data out and then deserialize it. Unfortunately ASP.NET Core doesn't let you just capture 'raw' data in any meaningful way just by way of method parameters. So how do we get at the raw data? Reading Request.Body for Raw Data However, this makes sense if you think about it: MVC has mappings for specific content types and if you pass data that doesn't fit those content types it can't convert the data, so it assumes there's no matching endpoint that can handle the request. It's not super obvious and I know this can trip up the unsuspecting Newbie who expects raw content to be mapped.
The endpoint exists, but MVC doesn't know what to do with the text/plain content or how to map it and so it fails with a 404 Not Found. I'm essentially doing the same thing as in the first request, except I'm not sending JSON content type but plain text. Public string PlainStringBody( string content) So if you trying to send this: POST HTTP/1.1 Raw data is not directly mappable to controller parameters by default. ASP.NET Core handles only what it knows, which by default is JSON and Form data. If you want to send a RAW string or binary data and you want to pick that up as part of your request things get more complicated. I say this because I've forgotten it plenty of times and scratched my head wondering why request data doesn't make it to my method or why requests fail outright with 404 responses. It's easy to forget and not really obvious that it should be there. Make sure you add to any parameter that tries to read data from the POST body and maps it. Note that the string sent is not a raw string, but rather a JSON string as it includes the wrapping quotes: "Windy Rivers are the Best!" This works to retrieve the JSON string as a plain string. Public string JsonStringBody( string content)įigure 1 - JSON String inputs thankfully capture as strings in ASP.NET Core You can accept a string parameter and post JSON data from the client pretty easily.
Lets start with a non-raw request, but rather with posting a string as JSON since that is very common. To check this out I created a new stock ASP.NET Core Web API project and changed the default ValuesController to this sample controller to start with: public class BodyTypesController : Controller The good news is that it's quite a bit easier to create custom formatters in ASP.NET Core that let you customize how to handle 'unknown' content types in your controllers. The way the Conneg algorithm works in regards to generic data formats is roughly the same as it was with Web API. Unfortunately the process to get at raw request data is rather indirect, with no direct way to receive raw data into Controller action parameters and that hasn't really changed in ASP.NET Core's MVC/API implementation. A few years back I wrote a post about Accepting Raw Request Content with ASP.NET Web API.