Search code examples
ruby-on-railsjsonposttyphoeus

How to send a http post request to a rails app to get json response


For GET request its:-

 response = Typhoeus::Request.get("http://localhost:3000/users/1.json?oauth_token=12")

This returns Json response perfectly.

for Post request:-

   response = Typhoeus::Request.post("http://localhost:3000/users/1.json?oauth_token=12",:params => {'[user][city]' => params[:location]})

is not working...

This is returning routing error.

Update:--

FOr login this api post call is working..

     response = Typhoeus::Request.post(API_SERVER_ADDRESS + "user_sessions.json" + API_OAUTH_TOKEN, :params => {'[user_session][email]' => params[:email], '[user_session][password]' =>params[:password]})

In routes its

resources :users

and also web http request is working perfectly fine..

UPDATE

For example http request from rails log is:--

   Parameters: {"commit"=>"Update", "authenticity_token"=>"8nvzCd0GF9IxjMcTfHOMJTPnycVPNIENMoMff8w4qAI=", "utf8"=>"✓", "id"=>"1", "user"=>{ "city"=>"abc"}}

Now i want to sent same kind of request..


Solution

  • Here is the solution

    From this

     response = Typhoeus::Request.put(API_SERVER_ADDRESS + "users/" +user_id + ".json" ,:params => {:oauth_token=>'12', :user=>{:city => params[:location]}})