DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

🛠️ Building a Simple Rails App to Display HTTP Request Headers

April 25, 2025

I recently built a small Ruby on Rails app with a simple but insightful purpose: 👉 Display all the HTTP headers your browser sends when visiting a page.

It’s a straightforward project, but it helped me explore how HTTP headers work and provided a neat way to learn more about browser requests in Rails.


đź’ˇ Why Show HTTP Headers?

Article content

HTTP headers are often overlooked, but they carry a lot of important information. They provide context for the request, including technical details about the browser, the operating system, and even user preferences.


đź’¬ Need Help Collecting Visitor Information in Your Web Application?

If you’re looking to improve how you collect information from your visitors and make better use of HTTP headers or other data, I can help. Let’s chat about how you can enhance your web applications with smart data collection techniques and integrations.

👉 Get in touch


I categorized the headers into two groups to make them easier to understand:

đź–Ą System / Technical Headers

These headers reveal information about the technical environment of the request — things like the browser version, the operating system, compression methods, and caching preferences. For example:

  • HTTP_USER_AGENT: The browser, OS, and device info.
  • HTTP_ACCEPT_ENCODING: Supported encoding formats.
  • HTTP_CONNECTION: The type of connection being used.
  • HTTP_HOST: The host (domain or IP) being requested.
  • HTTP_CACHE_CONTROL: Caching rules for the request.

đź§‘ User / Personal Browser Preferences

These headers reflect personal user settings and choices — helpful for tailoring the user experience or understanding user behavior. For example:

  • HTTP_ACCEPT_LANGUAGE: The preferred language of the user.
  • HTTP_ACCEPT: What content types the browser can accept.
  • HTTP_REFERER: The page the user came from (if available).
  • HTTP_COOKIE: Any cookies sent with the request.
  • HTTP_DNT: “Do Not Track” preference of the user.

đź§Ş What I Built (in Rails)

I created a very simple Rails app to display the HTTP headers sent by the browser. Here’s how I did it:

In the controller, I extracted the headers with this snippet:

@headers = request.headers.env.select { |k, _| k.start_with?('HTTP_') }
Enter fullscreen mode Exit fullscreen mode

I then split the headers into two categories — System Info and User Preferences — and rendered them in a clean HTML table in the view.


đź”§ How to Try This Yourself

If you want to try this out, here’s a quick guide:

  • Create a new Rails app: rails new headers_app –skip-active-record
  • Generate a controller to display the headers: rails generate controller headers index
  • Set up a route to /headers in your config/routes.rb:
get 'headers/index'
Enter fullscreen mode Exit fullscreen mode
  • In the headers_controller.rb, extract the headers like this:
@headers = request.headers.env.select { |k, _| k.start_with?('HTTP_') }
Enter fullscreen mode Exit fullscreen mode
  • In the index.html.erb view, display the headers in two tables (one for System Info and one for User Preferences).

That’s it! You now have a quick way to inspect the HTTP headers coming from your browser.


đź§  Final Thoughts

Sometimes, the best way to learn is by building something simple and focused. This small project allowed me to revisit HTTP fundamentals and provided new insights into how browsers communicate with web servers. It also gave me ideas on how to use this information responsibly in development, especially for debugging or tailoring user experiences.

Let me know if you’d like to see the full code or want to discuss how we can make use of these headers in more complex Rails applications!

Article content

Top comments (0)