1

I want to read the header of a request coming in to a Rails Controller...

How can I read in the header of the request to see what the request expects/wants back (in terms of formats) and send back data in that format?

example:

#in the controller receiving the request
def receive_req
   request.head #read value from header
   #if req wants json, format to json else, format to html etc...
   res = response
   res.head = "set appropriate header values"
   res.body = "data to send back in the body"
end


#in the controller making the req
def send_request
  Net::HTTP.post("/receive_req", "data", header_values)
  render #{response.body}
end

1 Answer 1

3

Use the request.headers hash as described here.

Of course, you might also choose to define your routes in such a manner that each endpoint provides output in only one format. Whatever works for your application.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.