JSON Rails 中的 Rest-Client 授权

JSON Rest-Client Authorization in Rails

出于某种原因,当我 运行 在我的 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

这是我的堆栈跟踪:

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 并尝试了列出参数的不同方法,moving/removing content_type 等。对我来说没有任何效果。

您不需要像那样嵌套参数。只是做:

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

我注意到我的 content_type 没有更新,搜索了一下,Whosebug 帮我找到了。 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 的帮助。