Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing headers from Sinatra

I am trying to access the headers in a filter in sinatra. My request includes the header "HTTP_AUTH", however I can't access it. My filter is

before do
    halt 403 unless request['HTTP_AUTH'] == 'test'
end

It works correctly from my rack test.

browser.get '/mypath', "CONTENT_TYPE" => "application/json", "HTTP_AUTH" => 'test'

But when I try from other sources I can't access it. If I puts request.env I can see the token is in the request, but I can't access it.

"HTTP_CONNECTION"=>"close", 
"HTTP_AUTH"=>"test", 
"HTTP_ACCEPT"=>"application/json", 

What am I doing wrong?

like image 770
Jim Jeffries Avatar asked Sep 04 '14 08:09

Jim Jeffries


People also ask

How do I see all headers?

To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.

How do I add a header to my server?

In the connections pane, expand the node for the server, and then expand Sites. Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add.


1 Answers

I just wanted to add that if you use headers it will not show custom headers. For example, I set up a custom header named X-CSRF-Token which I send on every AJAX request, and this one won't show up in that hash. If you need to access custom headers, you will find them through request.env like:

post '/' do
  header_token = request.env["HTTP_X_CSRF_TOKEN"]
end

(Notice how rack changes X-CSRF-Token to HTTP_X_CSRF_TOKEN)

like image 193
Diego Alejandro Molina Leiva Avatar answered Sep 28 '22 06:09

Diego Alejandro Molina Leiva