1

对于 GET 请求,它的:-

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

这将完美地返回 Json 响应。

对于发布请求:-

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

不管用...

这是返回路由错误。

更新: -

对于登录这个 api post call 正在工作..

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

在路线中

resources :users

而且网络 http 请求也工作得很好..

更新

例如来自 rails 日志的 http 请求是:-

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

现在我想发送相同类型的请求..

4

3 回答 3

1

:params 参数应该是你的参数的哈希值,意思是键值对,所以可能是这样的:

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

...或类似的东西 - 无论参数是什么,无论值是什么。我认为,您的原件并没有转化为您想要做的有意义的散列。

此外,请检查您的路由以确保您尝试执行的操作正确路由。

于 2011-06-23T19:24:35.287 回答
1

这是解决方案

从此_

 response = Typhoeus::Request.put(API_SERVER_ADDRESS + "users/" +user_id + ".json" ,:params => {:oauth_token=>'12', :user=>{:city => params[:location]}})
于 2011-06-23T21:19:03.680 回答
0

确保您在文件中声明了单独的POST路线routes.rb。即使 URL 相同,不同的 HTTP 方法也需要不同的路由。

默认情况下,使用resources :users为您提供以下内容:

GET     /users/new        # new
POST    /users            # create
GET     /users/:id        # show
GET     /users/:id/edit   # edit
PUT     /users/:id        # update
DELETE  /users/:id        # destroy
于 2011-06-23T19:25:53.463 回答