Way to get request headers outside of header matcher?

270 views
Skip to first unread message

Andrew Burleson

unread,
Jul 11, 2016, 5:10:00 PM7/11/16
to Roda
Hi Jeremy,

As I understand it (and as we previously discussed), the way to check headers on requests is to use a header matcher.

Now, in my app's error block, I'd like to render HTML for browsers and return JSON (or an empty body) for non-browsers.

Here's a sample of what I'm talking about:

error do |e|
  if e.is_a?(Sequel::NoMatchingRow)
    response.status = 404
    # if request.accept?('text/html')
    #   view 'not_found'
    # else
    #   JSON.generate({message: "Not Found"})
    # end
  end
end

This doesn't currently work since request has no accept? method. Is there a good alternative here?

Thanks!

Andrew

Jeremy Evans

unread,
Jul 11, 2016, 11:10:04 PM7/11/16
to Roda
Early today I pushed up a type_routing plugin supporting this:

 request.json{JSON.generate({message: "Not Found"})}
 request.html{view 'not_found'}

It should be released in the next version, which should happen later this week.

With the current version, there is also the :accept matcher in the header_matchers plugin:

  r.on(:accept=>'text/html'){view 'not_found'}

Thanks,
Jeremy

Andrew Burleson

unread,
Jul 12, 2016, 10:56:45 AM7/12/16
to Roda
Hi Jeremy,

That looks like a nice addition, thanks!

I'm still a bit curious, it seems like there's no very direct way to get at the header info itself. Is there a reason that you're averse to providing something like a `request.headers` method that just has a hash of headers? Or am I misinterpreting that?

Thanks so much for all your hard work!
Andrew

Jeremy Evans

unread,
Jul 12, 2016, 12:32:42 PM7/12/16
to Roda
On Tuesday, July 12, 2016 at 7:56:45 AM UTC-7, Andrew Burleson wrote:
Hi Jeremy,

That looks like a nice addition, thanks!

I'm still a bit curious, it seems like there's no very direct way to get at the header info itself. Is there a reason that you're averse to providing something like a `request.headers` method that just has a hash of headers? Or am I misinterpreting that?

You can always get access to request headers via env, using the CGI-mangled names:

  env['HTTP_ACCEPT'] # Accept Header

Also in the master branch is a request_headers plugin, which allows you to do:

  request.headers['Accept']

Thanks,
Jeremy

Andrew Burleson

unread,
Jul 12, 2016, 12:33:42 PM7/12/16
to ruby...@googlegroups.com
Got it. Thanks so much!

Andrew


--
You received this message because you are subscribed to the Google Groups "Roda" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-roda+...@googlegroups.com.
To post to this group, send email to ruby...@googlegroups.com.
Visit this group at https://groups.google.com/group/ruby-roda.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-roda/87e7c514-b561-4327-a730-fcea076e2238%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages