5

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.

Nabeel
  • 147
  • 1
  • 8
griftopia
  • 135
  • 1
  • 3
  • 10
  • 1
    According to https://stackoverflow.com/questions/978061/http-get-with-request-body it is never a good idea to have a GET with a body. – stannius Oct 25 '18 at 22:08
  • 3
    Possible duplicate of [WebApi: How to make get request with complex Object?](https://stackoverflow.com/questions/50215288/webapi-how-to-make-get-request-with-complex-object) – stannius Oct 25 '18 at 22:13
  • @stannius. Okay I get it. No pun intended. Can you please tell me how I can send JSON object on the URL? – griftopia Oct 26 '18 at 19:20
  • My problem is that my JSON object is not fixed. It's like a dictionary. It can have multiple parameters. sometimes 1 sometimes 4. That's why I need JSON object on first place. However when I embed it in the URL it does not work using [FromUri]. I always get a null object, – griftopia Oct 26 '18 at 19:23
  • How are you creating the request? jQuery? – stannius Oct 26 '18 at 19:30
  • @stannius No. Like I indicated above I tried Swagger, but apparently there is known bug/deficiency with it which will not allow. And I tried typing it on the URL. I know if I could put it in HTTP Body like I do for Post() methods, it will parse it into a Dictionary object. So I either need (a) use [FromUri] and figure out how to type JSON object on URL with Get OR (b) use [FromBody] and somehow get it to work with Get. Neither option is working for me. – griftopia Oct 27 '18 at 00:07
  • If you look at the second link I commented above, it seems that you need to add each of the object's properties as a query string parameter: "url.../?a=A&b=B" You can have a dynamic number of parameters. I imagine any properties you don't pass as parameters get initialized to their default values. – stannius Oct 27 '18 at 03:26
  • Just to be clear: You are much more likely to be successful with "use [FromUri] and figure out how to type JSON object on URL with Get". – stannius Oct 29 '18 at 15:10
  • @stannius. Here you loud and clear. However, I do know how to type a JSON object string, LOL. There's something else I must be doing wrong, so visiting this again today after a break. Will update ASAP. – griftopia Oct 31 '18 at 11:44
  • 1
    So taking a break helped (I think). The problem is I'm used to having Dictionary in my Model objects when I'm POSTing JSON data using [FromBody]. With [FromUri] I have to get the data as raw string and then explicitly convert to Dictionary. I thought Web Api will do that for me, but it didn't. So for now at least I'm past my problem. – griftopia Oct 31 '18 at 13:12
  • you can convert your JSON parameter to base64 format and passing it as normal string parameter in get method, then in service side, convert this parameter from base64 to string (raw json) , now you can deserialize this json to any class that you know – Bijan Ghasemi Nov 02 '18 at 05:26

0 Answers0