Using [FromBody] in GET Method of WebApi Controller

griftopia picture griftopia · Oct 25, 2018 · Viewed 8.4k times · Source

So I know how to use [FromUri] and [FromBody] on a "Post" method of my WebApi Controller. Needless to say [FromUri] on a "Get" method is unnecessary but explicit, and works well. My problem is I want to WebApi to parse my HttpBody on a "Get" method to populate my object parameter. Like this...

public IHttpActionResult Get([FromBody]Customer c) {
   if (c is null) {
      // problem :-(
   }
}

First, let me explain why I want to do this. It is because my "Get" URL is complex. I need to include JSON object on it. So while I know it is not perfect REST to use HTTP Body for the payload on a "Get", I would like to do it.

Second, for those who think I should just include it on the URL...okay, fine. However, then please tell me how to test because I tried typing JSON object on the URL, the browser quoted the heck out of it, BUT even with [FromUri] on my "Get" WebApi Controller method, I can't get it to parse. I also tried curl through Swagger, but same issue.

My JSON object is simply { "a" : "A", "b" : "B" }.

Thanks in advance for any help.

Answer