0

I send PUT request to some service with RestClient gem. I do it like:

RestClient.put('http:/app.com/resource/:id.json', { app_token: 'xxx', resource: { status: 'NNN' }})

But the answer JSON is empty (doesn't return what I need). If I send the request like this:

RestClient.put('http:/app.com/resource/:id.json?app_token=XXX&resource[status]=NNN', {})

then all is OK (JSON is normal). What is wrong with my code? Thanks!


P.S. Log:

1) RestClient.put "http:/app.com/resource/:id.json", "app_token=XXX&resource[status]=NNN", "Accept"=>"/; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"73", "Content-Type"=>"application/x-www-form-urlencoded"

# => 200 OK | application/json 12 bytes (empty JSON)

2) RestClient.put "http:/app.com/resource/:id.json?app_token=XXX&resource[status]=NNN", "Accept"=>"/; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate"

# => 200 OK | application/json 37 bytes (valid JSON)

2 Answers2

0

From doc I think it should be written as:

RestClient.put('http:/app.com/resource/:id.json', nil,{params: { app_token: 'xxx', resource: { status: 'NNN' }}})

See https://github.com/rest-client/rest-client#query-parameters

tomsoft
  • 4,448
  • 5
  • 28
  • 35
  • tyied to, but get the same result :( – Andrew Smith Aug 27 '14 at 09:24
  • sorry, for a post, did you try this: RestClient.put('http:/app.com/resource/:id.json', nil,{params: { app_token: 'xxx', resource: { status: 'NNN' }}}) (I've updated my answer), just add the nil or "" in the payload – tomsoft Aug 27 '14 at 09:38
  • Yes, this is nice! Only one problem: rest_client generates next request: RestClient.put "http:/app.com/resource/:id.json?app_token=XXX&&resource=%7B%3Astatus%3D%3E%22NNN%22%7D", "", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"0", "Content-Type"=>"application/x-www-form-urlencoded" – Andrew Smith Aug 27 '14 at 11:31
  • It means, that RestClient send next parameters: `resource={:status=>"NNN"}` – Andrew Smith Aug 27 '14 at 11:37
  • I can send `RestClient.put('http:/app.com/resource/:id.json', nil,{params: { app_token: 'xxx', 'resource[status]' => 'NNN' }})`, but it is some monkey patch :| – Andrew Smith Aug 27 '14 at 11:46
  • no, the second parameter is the payload of the post. Usually, if you do a post request, that's because you "post" something and this is usually not empty , this make sense... – tomsoft Aug 27 '14 at 12:07
  • I mean that `'resource[status]' => 'NNN'` is the monkey patch. I wanna clear syntax like simple hash, not the strings... – Andrew Smith Aug 27 '14 at 12:24
  • ah I see..... there should be something in rails somewhere to do this, not sure if your app is using rails? – tomsoft Aug 27 '14 at 13:13
  • .to_query does the job, but it's add by rails. Exemple: {params:{a:1}}.to_query returns "params%5Ba%5D=1" – tomsoft Aug 27 '14 at 13:35
0

Why don't you try this one out? Much cleaner syntax.

@options = { 
    params: { 
        app_token: 'xxx', 
        resource: { 
            status: 'NNN'
        }
    } 
}

RestClient.put("http:/app.com/resource/:id.json", @options)
sidegeeks
  • 1,021
  • 3
  • 12
  • 17