Accessing headers from Sinatra
Try use before
block with headers
method:
before do
headers "HTTP_AUTH" => "test"
headers "Content-Type" => "text/html; charset=utf-8"
end
or in request:
get '/' do
headers['HTTP_AUTH'] = "test"
headers['Cache-Control'] = 'public, max-age=600'
puts headers # show headers on this request
end
Use headers
with is just hash
How to read headers in Sinatra?
Use request.env
for accessing request headers EX: header_token = request.env["HTTP_X_CSRF_TOKEN"]
How to access *incoming* headers in Sinatra?
Have you tried adding HTTP to the header name? So it would be request.env["HTTP_ MyHeader"]
This is part of the rack spec.
Authenticate using headers in Sinatra
The answer is simple:
By default, Sinatra listens on port 4567, so I just made sure it's binding to all the interfaces just in case i want to call it from its external IP address and disabled any verbose error output as follow:
listener.rb
require "sinatra"
set :bind, "0.0.0.0"
disable :show_exceptions
disable :raise_errors
post "/" do
# Check if the header matches
# If it did not match then halt and return code 401 Unauthorized
if request.env["HTTP_custom_header_name"] != "verystrongpassword"
halt 401
end
#the rest of your code goes here
status :ok
end
Note that when comparing header value , HTTP must always be included and then goes the name of your header - Link
Example
require "sinatra"
set :bind, "0.0.0.0"
disable :show_exceptions
disable :raise_errors
post "/" do
# Check if the header matches
# If it did not match then halt and return code 401 Unauthorized
if request.env["HTTP_X_GIT_SECRET"] != "d4c74594d841139328695756648b6bd6"
halt 401
end
data = JSON.parse request.body.read
p data
status :ok
end
Where X_GIT_SECRET is a header name
Extra
if you don't know what is the name of the header being sent to sinatra then you may check all the request content by putting the following before the if statement up:
p request.env
and then try sending a request again, find your header and do the comparison based on it.
Note: status :ok
aka 200 OK, was set at the end of the block because when someone sends a request to sinatra it should return something, else an 500 internal server error would occur.
Where should I set HTTP headers, such as Expires?
After talking though and answering this question and seeing the comment above, I think I have figured out the answer to my own question.
The whole point of nginx actually removes the first two options.
That leads to Option #3. This is where all the other content config is set, such as gzip compression.
How to get Headers from request?
request.get_header('HTTP_X_GITHUB_HOOK_INSTALLATION_TARGET_TYPE')
# or
request.env['HTTP_X_GITHUB_HOOK_INSTALLATION_TARGET_TYPE']
It is worth mentioning that,sinatra will process custom header:
- add prefix
HTTP_
- capitalize every letter
- gsub
-
to_
You can use request.env
to access all headers.
For detail information about what variable will be added HTTP_
you can refer there
Related Topics
Rails Console Fails with 'Switch to Inspect Mode' in Windows
Why Does Psych Yaml Interpreter Add Line Breaks Around 80 Characters
Normal Variables VS Instance Variable in Ruby, Whats the Difference
How to Get Systemd to Restart Rails App with Puma
What's the Efficient Way to Multiply Two Arrays and Get Sum of Multiplied Values in Ruby
How to Host a Rails Web Application
How to Make Block Local Variables the Default in Ruby 1.9
How to Get The Pty.Spawn Child Exit Code
Alter Rails Params Hash from Rack Middleware
How to Retrieve Exif Information of an Image in Rails
Fresh Install of Rvm in Ubuntu Isn't Letting Me Install Gems (Zlib Error)
Run Selenium with Chrome Driver on Heroku: 'Cannot Find Chrome Binary'
How to Extend a Class from an Initializer and Have It Reload in Development Environment
How to Assert That No Route Matches in a Rails Integration Test
Anything Speaking Against the Bitnami.Org Ruby/Rails/Redmine Stack
Open a File Case-Insensitively in Ruby Under Linux