Search code examples
rubysinatraput

How does Sinatra's `put` work?


get in Sinatra displays whatever you want when you type the path into the URL. I don't put. How do you call put?

I am trying to run

put '/:name' do |name|
  puts "hello " + name
end

How do I actually call this? I type into my browser:

http://localhost:4567/examplename\

but when I read it in my terminal (cmd prompt), it tries to access it as get. What am I missing regarding how put works?


Solution

  • The put method corresponds with an HTTP PUT request. If you're making a GET, which is what the browser does by default, you should change that to:

    get '/:name' do |name|
      # ...
    end
    

    If you're talking about "how do I write to the browser" then you need this:

    get '/:name' do |name|
      "hello #{name}"
    end
    

    Don't write things to STDOUT with puts, just return the content you want to be sent. That's how Sinatra works.

    If you want to make a PUT request you need to tell your tool to use that method. For example, with curl -X PUT.