0

I'm trying to get the simple put and get running. Get works perfectly but I seem to get a 404 with a PUT.

Things I've tried:

  1. Wrote up code for a app.get "/users/:id" and localhost:3000/users/400 to print the user's id on the browser -> this works and it's a get
  2. Tried doing the same for a post -> app.post "/users/add" (code below) and did a "localhost:3000/users/add/11" but I get a 404.

My doubts:

  1. How can I test the post on a browser? How should the URL be? Where can I put info to be posted in the URL? Or should it not be specified in the URL? (This is confusing) I am guessing the lack of input data is giving me a 404.

I run this on the Sinatra framework and this what I see on the console:

INFO WEBrick::HTTPServer#start: pid=10137 port=3000 ::1 - - [19/Mar/2015:11:36:34 -0700] "GET /users/add HTTP/1.1" 403 - 0.0156

(Wait, shouldn't this be a POST??)

::1 - - [19/Mar/2015:11:36:45 -0700] "GET /users/add/11 HTTP/1.1" 404 688 0.0086

(My question on how I specify the data to be put)

::1 - - [19/Mar/2015:11:43:15 -0700] "GET /users/add/name=11 HTTP/1.1" 404 693 0.0018

My routes users.rb code:

module Routes
  module Users
    def self.registered(app)
      app.post '/users/add' do
         reqUserId = params[:id]
         "params id #{reqUserId}"
    end  
 end
end

Any help would be great. I'm super open to reading anything too.

Sir l33tname
  • 4,026
  • 6
  • 38
  • 49
premunk
  • 303
  • 1
  • 4
  • 18

1 Answers1

0

Well it looks like you got confused by a few things here.

First of all, put and post are not the same. PUT vs POST in REST Probably you want use post.

What you probably want is something like this:

get "/users/add" do
    erb :useradd
end

post "/users/add" do
    params[:userId]
    #render layout or redirect to a url
end

And your useradd erb template would look like this:

<form action="/users/add" method="post">
    <input name="userId" type="text" />
    <input type="submit" value="submit" id="submit" />
</form>

So your browser does a GET if you open a url like /users/add which renders your view. And if you press on submit the form with your input fields gets posted.

To test your Post routes you can also use something like curl:

curl --data "param1=value1&param2=value2" http://localhost:3000/users/add
Community
  • 1
  • 1
Sir l33tname
  • 4,026
  • 6
  • 38
  • 49