Accessing headers from Sinatra

放肆的年华 提交于 2019-12-03 06:22:51

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

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!