2

I am using http party gem to try and send some json to a web api I have running in Visual Studio.

The C# implementation of my Post API is as below:

 public string GetInfo([FromBody]MyClass[] postedValues)
 {
   //do stuff

If I configure fiddler to hit the method and within fiddler set the request header to be:

Content-Type: application/json; charset=utf-8

and then the request body to be:

[{"Id" : "1", "A" : "1", "B" : "1", "c" : "1"}, {"Id" : "2", "A" : "2", "B" : "2", "C" : "2"}]

when I hit execute the postedValues automatically maps the values from the json into the array - so for the above there will be 2 elements in the array - the Id in MyClass postedValues[0] will be 1 and the Id in postedValues[1] will be 2 and the same for all the other properties.

I want to hit the WebAPi from Ruby on Rails so I am using http party and building the request as below(note full http:// taken out of URL):

post('localhost/api/MyController/GetInfo', :body => [{:Id => '1',
                                                     :A => '1',
                                                     :B => '1',
                                                     :C => '1'}].to_json,
                         :headers => {'Content-Type' => 'application/json'})

However now if I put a breakpoint in my C# method postedValues is null. If I change the C# method to:

public string GetInfo([FromBody]dynamic postedValues)
{
       //do stuff

and run the Ruby code I do see values passed but would much prefer if the values were mapped automatically as they are when I hit it from Fiddler - is there something I am doing incorrect with http party?

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
Ctrl_Alt_Defeat
  • 3,933
  • 12
  • 66
  • 116
  • Can you see any difference between raw requests (the manual vs. ruby)? – Uri Agassi Apr 07 '14 at 13:48
  • Hi @UriAgassi - with Fiddler request I can see the JSON being sent (from Fiddler) - with the Ruby Request - it is running on 127.0.0.1:3000 - I dont see anything in Fiddler getting sent when I hit my breakpoint.. – Ctrl_Alt_Defeat Apr 07 '14 at 14:05
  • I'm not sure you can use fiddler to see the raw request - try http://stackoverflow.com/a/7292900/1120015 – Uri Agassi Apr 07 '14 at 14:10
  • @KOL: You can use `Request.Content` inside the controller to access the raw request. Post the differences here :) – David Apr 07 '14 at 15:33
  • @David - thanks for the tip - i'll try that and Edit my Question with results – Ctrl_Alt_Defeat Apr 07 '14 at 19:21

1 Answers1

0

I ended up pulling the request body out into a class level variable and it worked mapping the values as expected - hopefully this is useful to someone else.

  @request_body = [{:Id => '1',
                    :A => '1',
                    :B => '1',
                    :C=> '1'}]

    post('localhost:50544/api/MyController/GetInfo', :body => @request_body.to_json,
                                     :headers => {'Content-Type' => 'application/json'})
Ctrl_Alt_Defeat
  • 3,933
  • 12
  • 66
  • 116