# Behaviour Accept header vs. respond\_to

**URL:** <https://discuss.rubyonrails.org/t/behaviour-accept-header-vs-respond-to/35418>\
**Category:** rubyonrails-talk\
**Created:** [October 16, 2008, 11:26am UTC](https://discuss.rubyonrails.org/t/behaviour-accept-header-vs-respond-to/35418 "2008-10-16T11:26:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sjoerd\_Tieleman](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@Sjoerd\_Tieleman](https://discuss.rubyonrails.org/u/Sjoerd_Tieleman)\
**Post date:** [October 16, 2008, 11:26am UTC](https://discuss.rubyonrails.org/t/behaviour-accept-header-vs-respond-to/35418/1 "2008-10-16T11:26:31Z")

</div>

Hi list,

I noticed some strange behaviour while testing out a couple of things regarding building an API. Suppose I have a very basic Rails app with just one resource "widgets", I'd have the following URLs:

[http://somehost/widgets](http://somehost/widgets)[http://somehost/widgets.xml](http://somehost/widgets.xml)

First URL shows the widgets index using HTML, the second one using XML. If however, instead of requesting "widgets.xml", I'm requesting just the first URL but with setting the HTTP Accept header to "Accept: application/xml" I would expect to get results using my XML builder. However, it returns HTML content (and sets Content-Type to "text/ html").

Now, this is clearly different behaviour for what would be identical requests. However, if I include a respond\_to block in my controller:

respond\_to do |format| &nbsp;&nbsp;format.html &nbsp;&nbsp;format.xml end

It works just as expected: setting the Accept header does in fact influence what is returned (XML or HTML). However, this means I have to include a respond\_to block in every controller method, which I think is undesirable.

So in conclusion, format switching works without respond\_to block, but only if you specify an extension in the URL, not if you use the "Accept:" header. If you include a respond\_to block, then format switching works also by using the "Accept:" header.

Can someone explain if (and why) this behaviour is correct? Using Rails 2.1.1.

Kind regards,

Sjoerd Tieleman

---

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jeff1/32/1170_2.png) [@Jeff1](https://discuss.rubyonrails.org/u/Jeff1)\
**Post date:** [October 16, 2008, 3:11pm UTC](https://discuss.rubyonrails.org/t/behaviour-accept-header-vs-respond-to/35418/2 "2008-10-16T15:11:29Z")

</div>

I believe it's because in the first case, /widgets.xml, your routes.rb is helping to determine the :format for you (assuming you've still got the default routes in your routes.rb).

Rails won't look at the Accept header automatically - you have to use a respond\_to block to do that. As for why that is, I'm not sure - I agree it might be nice for Rails to set the :format based on the Accept header as well as by route pattern.

Jeff

purpleworkshops.com softiesonrails.com
