Skip to content

Instantly share code, notes, and snippets.

@straypacket
Last active December 20, 2015 16:29
Show Gist options
  • Save straypacket/6162306 to your computer and use it in GitHub Desktop.
Save straypacket/6162306 to your computer and use it in GitHub Desktop.
Sample Sinatra server
##
# Call with:
# curl -i -H "Accept: application/json" "http://localhost:4567/?foo=1"
# or post with:
# curl -X POST -H "Accept: application/json" "http://localhost:4567/?foo=1" --data "foo=2"
##
require 'rubygems'
require 'sinatra'
require 'json'
encoding_options = {
:invalid => :replace, # Replace invalid byte sequences
:undef => :replace, # Replace anything not defined in ASCII
:replace => '', # Use a blank for those replacements
:universal_newline => true # Always break lines with \n
}
# Listen to all ports
set :bind, '0.0.0.0'
get '/' do
p params["foo"]
# Outputs 1
end
post '/' do
p params["foo"]
# Outputs 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment