How to set headers of a JSON/XML response in 'format.json/xml { render :json/xml => @user.to_json/xml }'?
I am using Ruby on Rails 3 and I am trying to set va开发者_Python百科lues of a JSON/XML response.
In my controller I have
respond_to do |format|
format.xml { render :xml => @user.to_xml }
format.json { render :json => @user.to_json }
end
When I make a HTTP GET request for JSON/XML, it is set common values like these
header:
date:
- Fri, 18 Feb 2011 18:02:55 GMT
server:
- Apache ...
etag:
- "\"0dbfd0ec23934921144bd57d383db443\""
cache-control:
- max-age=0, private, must-revalidate
x-ua-compatible:
- IE=Edge
x-runtime:
- "0.033209"
status:
- "200"
transfer-encoding:
- chunked
content-type:
- application/json; charset=utf-8 #or application/xml; charset=utf-8
http_version: "1.1"
message: OK
read: true
I would like to add/set header
values and add new parameters like message2
or header2
.
How can I do that in format.json/xml { render :json/xml => @user.to_json/xml }
syntax?
The format.foo { render ... }
thing takes a block. You can put whatever you want there:
format.json do
response['X-Message-1'] = 'Hello'
render :json => @user.to_json
end
精彩评论