简体   繁体   中英

Render data into headers with RoR

I'm working on a small REST API, written in RoR (3.2), that I'll use behind a Varnish Cache instance.

This API will be called by Varnish's cURL VMOD to handle users count limit.

The big deal is that Varnish is not good at handling HTTP data, except for headers.

So I'd like to return data fields as HTTP headers (3 fields max), but I'm not sure about how to do this in a proper MVC way.

Could headers be considered as a valid view or render format ?

Should headers be written in the controller or in the view ? (knowing that returned data will come from 2 models).

Also note that I'll have to handle common html and json outputs in a future stage.

I know this will be a dirty hack anyway, but believe me : asking Varnish to work on response body, even worse on json or XML, would be x10 more dirty...

Thanks !

Stick to headers if you want a light weight application without invoking the View pipeline.

API calls use headers to send information to the client (keeps it simple).

Headers should be written in the controller before invoking the view since the HTTP headers go before the body.

You can currently do render => :nothing for the views and once you want to integrate json or xml, you can add the respond_to action .

I would say this is something you could set in the the controller. Take a look at this and see if it helps you out:

http://work.j832.com/2008/10/ruby-rails-render-and-content-type-http.html

I suggest doing this in the controller, as it sounds as if the headers will be independent of the view you render.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM