测试curl POST请求

时间:2014-11-24 19:12:50

标签: ruby-on-rails ruby curl

我是cURL的新手,一直在谷歌上寻求帮助,但却找不到我需要的东西。

我在http://127.0.0.1:3000/api/v1/1/listen有一个端点,我需要发送电子邮件地址。 (如果您想知道,/1/之前的/listen是用户ID,暂时是硬编码的。

尝试:

curl -d "test%40example.com" http://127.0.0.1:3000/api/v1/apps/1/listen

curl -H 'Host: appName.loc' --data "email=test%40example.com" http://127.0.0.1:3000/api/v1/apps/1/listen

都没有奏效。我确定它很小但是任何帮助都会受到赞赏!

另外,控制器代码:

module Api
  module V1
    class ListenersController < ApiController

      def listen
        debugger
        a = 10 # to catch the debugger
      end

    end
  end
end

谢谢!

编辑:

查看我的服务器标签,这是我运行curl -X POST --data 'email=teste%40example.com' http://127.0.0.1:3000/api/v1/1/listen

时显示的内容
Started POST "/api/v1/1/listen" for 127.0.0.1 at 2014-11-24 11:26:57 -0800

ActionController::RoutingError (No route matches [POST] "/api/v1/1/listen"):
  actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/application.rb:144:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/zackshapiro/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /Users/zackshapiro/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /Users/zackshapiro/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


  Rendered /Users/zackshapiro/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/gems/2.0.0/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.8ms)
  Rendered /Users/zackshapiro/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/gems/2.0.0/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (1.1ms)
  Rendered /Users/zackshapiro/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/gems/2.0.0/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (0.8ms)
  Rendered /Users/zackshapiro/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/gems/2.0.0/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (16.0ms)

路线:

namespace :api do
    namespace :v1 do
      resources :apps do
        post '/listen', to: 'listeners#listen', as: :listen
      end
    end
  end

           Prefix Verb   URI Pattern                           Controller#Action
api_v1_app_listen POST   /api/v1/apps/:app_id/listen(.:format) api/v1/listeners#listen

3 个答案:

答案 0 :(得分:1)

没有cotes的网址:

curl --data 'email=teste%40example.com' http://127.0.0.1:3000/api/v1/1/listen

你期待什么样的回应?也许是一个json?

curl --data 'email=teste%40example.com' http://127.0.0.1:3000/api/v1/1/listen.json

你可能有类似的东西:

respond_to do |format|
  format.html # listen.html.erb
  format.json { render json: @something }
end

答案 1 :(得分:1)

您应该为routes.rb添加匹配的POST路由:

您正在向/api/v1/1/listen发帖,但您只定义了/api/v1/apps/:app_id/listen(.:format)之类的URI模式。您的cURL网址中缺少apps

答案 2 :(得分:0)

尝试添加&#34; -X POST&#34;卷曲参数。