Accessing raw post data with raw_post in Rails

How to Access Raw POST Data in Rails

This post shows how you can access the raw, unaltered request body using a 20-year old method in the Rails framework (from the founder of Shopify). The `raw_post` method reads the request body, and is useful for web services or API controllers that need to directly work with raw requests.

1 min read
I haven't published any new posts in the past few weeks because we recently welcomed a baby girl into the family 😊, and combined with all the ongoing clientwork, it has become quite challenging to find quality time for writing. I really appreciate your patience though, and be assured that once things settle down, I will return to a regular posting schedule ✌️

If you are building an API or any sort of web services, you often want to work with the raw, unchanged request body. For a long time, I used to add this helper method in my Rails controllers to access the the incoming request body.

def request_body
  @request_body ||= (
    request.body.rewind
    request.body.read
  )
end

Recently, in one of the code reviews at work, I came across the raw_post method in Rails which does this for you. Here's what the method is doing behind the scenes.

# actionpack/lib/action_dispatch/http/request.rb

def raw_post
  unless has_header? "RAW_POST_DATA"
    set_header("RAW_POST_DATA", read_body_stream)
    body_stream.rewind if body_stream.respond_to?(:rewind)
  end
  get_header "RAW_POST_DATA"
end

def read_body_stream
  body_stream.rewind if body_stream.respond_to?(:rewind)
  return body_stream.read if headers.key?("Transfer-Encoding") # Read body stream until EOF if "Transfer-Encoding" is present
  body_stream.read(content_length)
end

In your Rails controller, you can access this method as follows:

request.raw_post

I am surprised I never ran into this method before, as it has existed in Rails for over two decades, and was introduced by none other than Tobias Lütke, the founder of Shopify! Quite a piece of open source history, isn't it?


That's a wrap. I hope you found this article helpful and you learned something new.

As always, if you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or send me an email. I reply to all emails I get from developers, and I look forward to hearing from you.

If you'd like to receive future articles directly in your email, please subscribe to my blog. Your email is respected, never shared, rented, sold or spammed. If you're already a subscriber, thank you.