Guides · Ruby · Net::HTTP

Capture a JavaScript-heavy page after it settles in Ruby with Net::HTTP

Render a client-side app too early and the capture shows the loading state, not the page: skeleton placeholders where cards should be, a spinner where the chart goes, grey boxes where lazy images land. The page "loaded" — the load event fired — but the app was still fetching its data. "wait_until": "networkidle" moves the goalpost: the render proceeds only after the page stops making network requests for a quiet period.

Below is a complete, runnable Ruby program using Net::HTTP, no third-party dependency needed. It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.

require "json"
require "net/http"
require "uri"

uri = URI("https://snapdok.io/v1/render")

res = Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 90) do |http|
  req = Net::HTTP::Post.new(uri)
  req["Authorization"] = "Bearer #{ENV.fetch("SNAPDOK_KEY")}"
  req["Content-Type"] = "application/json"
  req.body = JSON.generate({
    url: "https://your-app.com/report/42",
    format: "png",
    full_page: true,
    wait_until: "networkidle",
    delay: 500,
})
  http.request(req)
end

raise "snapdok error #{res.code}: #{res.body}" unless res.is_a?(Net::HTTPSuccess)

File.binwrite("settled.png", res.body)

The four stages, in order of patience: commit (bytes started arriving), domcontentloaded (HTML parsed), load (the default — static assets done), and networkidle (the network went quiet, which for an SPA usually means data fetched and rendered). delay then adds a fixed pause on top — the 500 ms here covers entrance animations and fade-ins that happen after the last fetch.

The cost of patience is time: networkidle on a page with analytics beacons or long-polling can take a while to go quiet, and a page that never goes quiet will run into timeout (a 504, never metered). If your page exposes a reliable "ready" element, wait_for_selector is the sharper tool — it waits for exactly the thing you care about, no more. There is a guide for it below.

Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.

Notes for Net::HTTP

Stdlib only — no gems. The traps Net::HTTP is famous for, all handled in the sample: TLS is not automatic (set use_ssl: true or you will POST in the clear to port 80 and get a redirect); the default read_timeout of 60 s is close enough to real render times that raising it is wise; and the response body is a binary-ready String, but write it with File.binwrite — plain File.write on Windows would mangle line-ending bytes in the PDF. Check is_a?(Net::HTTPSuccess): Net::HTTP never raises on HTTP error statuses.

Response headers worth reading

HeaderMeaning
X-Page-HeightMeasured document height for the full-page capture.
X-CacheIdentical requests within 24h replay for free.

Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.

Related guides

Same task, other stacks

More with Ruby + Net::HTTP