Zack Shapiro
Zack Shapiro

Reputation: 6998

Testing a curl POST request

I'm new to cURL and have been Googling around for help but haven't found what I need.

I have an endpoint at http://127.0.0.1:3000/api/v1/1/listen that I need to POST an email address to. (if you're wondering, the /1/ before /listen is the user id, that's hardcoded for the time being.

tried:

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

Neither worked. I'm sure it's something small but any help would be appreciated!

Also, controller code:

module Api
  module V1
    class ListenersController < ApiController

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

    end
  end
end

Thanks!

Edit:

Looked at my server tab, this is what shows up when I run 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)

Routes:

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

Upvotes: 1

Views: 459

Answers (3)

sealocal
sealocal

Reputation: 12447

You should add a matching POST route to your routes.rb:

You are POSTing to /api/v1/1/listen, but you have only defined a URI patter like /api/v1/apps/:app_id/listen(.:format). You are missing apps in your cURL url.

Upvotes: 1

gabrielhilal
gabrielhilal

Reputation: 10769

url without cotes:

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

what kind of response are u expecting? maybe a json?

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

You might have something like:

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

Upvotes: 1

bratsche
bratsche

Reputation: 2674

Try adding "-X POST" to the curl parameters.

Upvotes: 0

Related Questions