How to Dump an Http Request from Within Sinatra

How to dump an HTTP request from within Sinatra?

I run thin with the -D and -V flags when I want to debug 'things':

$ thin start -p 3000 -R config.ru -D -V

-D, --debug Set debbuging on
-V, --trace Set tracing on (log raw request/response)

If you are trying to get the raw output from a request, use the request method like:

  # app running on http://example.com/example
get '/foo' do
request.body # request body sent by the client (see below)
request.scheme # "http"
request.script_name # "/example"
request.path_info # "/foo"
request.port # 80
request.request_method # "GET"
request.query_string # ""
request.content_length # length of request.body
request.media_type # media type of request.body
request.host # "example.com"
request.get? # true (similar methods for other verbs)
request.form_data? # false
request["SOME_HEADER"] # value of SOME_HEADER header
request.referer # the referer of the client or '/'
request.user_agent # user agent (used by :agent condition)
request.cookies # hash of browser cookies
request.xhr? # is this an ajax request?
request.url # "http://example.com/example/foo"
request.path # "/example/foo"
request.ip # client IP address
request.secure? # false
request.env # raw env hash handed in by Rack
end

See "GETTING STARTED" for more information.

How to parse JSON request body in Sinatra just once and expose it to all routes?

Use a sinatra before handler:

before do
request.body.rewind
@request_payload = JSON.parse request.body.read
end

this will expose it to the current request handler. If you want it exposed to all handlers, put it in a superclass and extend that class in your handlers.

How to dump sinatra rack test exceptions to console?

This is combination of options that must be used and then it works.

set :raise_errors, true
set :dump_errors, false
set :show_exceptions, false

Sinatra doesn't know this ditty?

This is because swap is a POST endpoint. When you pull up a URL in your browser you are doing an HTTP GET.

If you want to see that the sinatra service is running and you can at least talk to it you could try hitting it with curl from the command line with the right POST parameters.



Related Topics



Leave a reply



Submit