Rails中的JSON Rest-Client授权

时间:2015-11-24 17:08:10

标签: ruby-on-rails ruby rest-client

由于某些原因,当我在Rails控制台中运行它时,它无法正常工作。

RestClient.post 'http://localhost/WebService/AuthenticateLogin', :content_type => :json, {:params => {:RuntimeEnvironment => 1, 'Email' => 'someone@example.com', 'Password' => 'Pa$$worD'}}

奇怪的是,当我用cURL做同样的事情时,它完全正常:

curl -H "Content-Type: application/json" -d '{"RuntimeEnvironment":1,"Email":"someone@example.com","Password":"Pa$$worD"}' -X POST http://localhost/WebService/AuthenticateLogin

这是我的stacktrace:

SyntaxError: (irb):33: syntax error, unexpected '\n', expecting =>
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

因为我认为这可能是相关的,所以我也使用了这个陈述:

RestClient.post 'http://localhost/WebService/AuthenticateLogin', :content_type => :json, :params => {:RuntimeEnvironment => 1, 'Email' => 'someone@example.com', 'Password' => 'Pa$$worD'}

将此作为我的堆栈跟踪:

RestClient.post "http://localhost/WebService/AuthenticateLogin", "content_type=json&params[RuntimeEnvironment]=1&params[Email]=someone%40example.com&params[Password]=Pa$$worD", "Accept"=>"*/*; q=0.5, application/
xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"114", "Content-Type"=>"application/x-www-form-urlencoded"
# => 400 BadRequest | text/html 2738 bytes
RestClient::BadRequest: 400 Bad Request
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:48:in `return!'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
        from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:852:in `start'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
        from (irb):34
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

我已经搜索了Stack并尝试了不同的方法来列出params,移动/删除content_type等等。没有什么对我有用。

2 个答案:

答案 0 :(得分:1)

你不需要像这样嵌套params。只是做:

RestClient.post 'http://localhost/WebService/AuthenticateLogin', {"RuntimeEnvironment" => 1, 'Email' => 'someone@example.com', 'Password' => 'Pa$$worD'}, :content_type => :json

答案 1 :(得分:0)

我注意到我的content_type没有被更新,搜索到它并且StackOverflow为我而来。 How do I make Ruby's RestClient gem respect content_type on post?

我基本上只是改变了我的要求:

RestClient.post 'http://localhost/WebService/AuthenticateLogin', '{"RuntimeEnvironment":1, "Email":"someone@example.com", "Password":"Pa$$worD"}', :content_type => "json"

我只是在我的参数周围加上引号:'{params}' 而不是在参数中使用=>,我改为冒号,就像我在cURL中使用的实际请求一样,一切都像魅力一样。

另请注意,我引用了"json" content_type。

感谢@rlarcombe的帮助。