ruby rest_client放置请求无法正常工作
问题描述:
我通过RestClient gem发送PUT请求到一些服务。我这样做:ruby rest_client放置请求无法正常工作
RestClient.put('http:/app.com/resource/:id.json', { app_token: 'xxx', resource: { status: 'NNN' }})
但答案JSON是空的(不会返回我所需要的)。如果我发送这样的请求:
RestClient.put('http:/app.com/resource/:id.json?app_token=XXX&resource[status]=NNN', {})
然后一切正常(JSON是正常的)。我的代码有什么问题?谢谢!
P.S.日志:
1)RestClient.put的 “http:/app.com/resource/:id.json”, “app_token = XXX &资源[状态] = NNN”, “接受”=>” /; q = 0.5,application/xml“,”Accept-Encoding“=>”gzip,deflate“,”Content-Length“=>”73“,”Content-Type“=>”application/x-www-form-urlencoded “
#=> 200 OK |应用/ JSON 12字节(空JSON)
2)RestClient.put的 “http:/app.com/resource/:id.json app_token = XXX &资源[状态] = NNN”,“接受“=>”/; q = 0.5,application/xml“,”Accept-Encoding“=>”gzip,deflate“
#=> 200 OK |应用程序/ JSON 37个字节(有效的JSON)
答
从文档,我认为它应该写成:
RestClient.put('http:/app.com/resource/:id.json', nil,{params: { app_token: 'xxx', resource: { status: 'NNN' }}})
见https://github.com/rest-client/rest-client#query-parameters
答
你为什么不试试这个?更清晰的语法。
@options = {
params: {
app_token: 'xxx',
resource: {
status: 'NNN'
}
}
}
RestClient.put("http:/app.com/resource/:id.json", @options)
tyied,但得到相同的结果:( – 2014-08-27 09:24:55
遗憾,因为一个帖子,你试试这个: RestClient.put( 'HTTP:/app.com/resource/:id.json',零,{params:{app_token:'xxx',resource:{status:'NNN'}}}) (我已经更新了我的答案),只需在负载中添加零或“” – tomsoft 2014-08-27 09:38:03
是的,这很好!只有一个问题:rest_client生成下一个请求: 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” “=>”application/x-www-form-urlencoded“ – 2014-08-27 11:31:34