Search code examples
ruby-on-railsrubyrest-client

JSON Rest-Client Authorization in Rails


For some reason when I run this in my Rails console it is not working.

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

The odd part is when I do the same with cURL it works perfectly fine:

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

Here is my 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>'

And because I think it may be relevant, I used this statement as well:

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

And got this as my stacktrace:

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>'

I have scoured Stack and tried different ways of listing out the params, moving/removing the content_type, etc. Nothing is working for me.


Solution

  • I noticed my content_type was not being updated, searched it up and StackOverflow came through for me. How do I make Ruby's RestClient gem respect content_type on post?

    I basically just changed my request to be:

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

    I merely added quotes around my params like so: '{params}' And instead of using => in the params, I changed to colons just like the actual request I used in cURL and everything worked like a charm.

    Also note I quoted the "json" content_type.

    Thanks @rlarcombe for your help.