Skip to content

Check What a Page Returns

Goal: make a URL monitor fail when your page is wrong, not only when it’s down. A response rule is a check on what the page sends back: a header, some text in the page, or a value in a JSON response.

A URL monitor already fails when the status code is wrong, such as a 503. A page can still answer 200 and be broken: an error message in place of your content, a security header that a change removed, or an API that answers {"status":"degraded"}. Response rules catch those.

  • You need a URL monitor. Rules are a setting of an existing URL monitor, so add the monitor first if you don’t have one. Other kinds of monitor have no rules.
  • Response rules need a paid plan. On a free plan the section says Response rules need a paid plan. with an Upgrade button, and offers nothing to add.

Each rule checks one of these, chosen under Checks:

ChecksWhat it looks atExample
HeaderOne response header, found by its nameStrict-Transport-Security is present
Body textThe page’s text and HTML, as your server sends itThe body contains Welcome back
JSON pathOne value in a JSON response, picked by a path such as $.status$.status equals "ok"
Valid JSONWhether the whole response is JSONThe body is valid JSON

On a monitor with Include JavaScript rendering on, body and JSON rules read the page as the browser shows it instead: see How the rules behave.

Under Comparison, a rule compares with contains, doesn’t contain, equals, doesn’t equal or matches, which takes a regular expression (a pattern for text, such as Order #\d+). Header and JSON path rules can also ask only whether the thing is present or is absent. Valid JSON has no comparison and no value.

  1. In Watching, choose the URL monitor, then Open monitor. Go to its Settings tab and find the Response rules section.

  2. Add a rule, either way:

    • Under Quick add, press a ready-made rule, such as HSTS is present or Response is valid JSON. Each one says what it catches. Some need finishing: Page contains text needs the text to look for, and JSON value equals needs your own path and value.
    • Or press Add rule. A new rule starts as a Header rule that asks whether the header is present.
  3. Fill in the rule. Each rule is numbered (Rule 1, Rule 2, …) and has:

    • Checks: what it looks at. Changing it starts the rule afresh, with its other fields cleared.
    • Header name for a header rule, or JSON path for a JSON path rule.
    • Comparison: how it compares.
    • Value: what to compare with, for any comparison other than is present and is absent. For a JSON path rule that equals or doesn’t equal, a number, true, false or null is compared as that kind of value; put quotes around one, such as "1", to compare it as text.
    • Ignore case, beside Value: tick it to compare without minding capital letters.

    A rule with a field missing or too long says what to fix under it.

  4. Add more rules if you need them, up to 10. They run in the order listed, and a new rule goes to the end; there’s no way to move one, so add them in the order you want them judged. To drop a rule, press its Remove.

  5. Press Save Monitor. The next check runs the rules.

When every rule passes, the check passes as before. When one fails, the check fails, and the incident and every alert say which rule failed, what it expected and what the response had:

Response rule 1 failed: expected header Strict-Transport-Security to be present, got no Strict-Transport-Security header

The alert links to the section of A Response Rule Failed for that kind of rule.

If DownBad won’t save the rules, the section says The rules were not saved: and why: a pattern it can’t use, or a plan without response rules.

  • They run after the status code passes, on the last answer after any redirects. A page that answers with an error code fails on that instead.
  • The first failing rule is the one reported. Rules after it aren’t judged on that check.
  • Header names match whatever their capital letters. Values compare with capital letters minding, unless the rule has Ignore case ticked. On a JSON path rule that equals or doesn’t equal, Ignore case counts only when both values are text.
  • A missing header or JSON value passes “is absent”, “doesn’t contain” and “doesn’t equal”, and fails every other comparison.
  • JSON “equals” compares the kind of value too, so the number 1 isn’t the text "1".
  • JSON paths start with $ and use .name, ['name'] and [0], such as $.data.items[0].status. Wildcards such as [*], filters and .. aren’t supported.
  • Body and JSON rules read the first megabyte (1 MB) of the response, on a monitor without JavaScript rendering. Text beyond that counts as missing, and a JSON response longer than that can’t be read, so every JSON rule on it fails.
  • Regular expressions run on RE2, which has no lookahead, lookbehind or backreferences such as \1. DownBad won’t save a pattern RE2 can’t use. A pattern can be up to 500 characters, and any other value up to 1000.
  • The reason shows up to 200 characters of what the response had.
  • DownBad reads the page as your server sends it. It isn’t signed in and keeps no cookies, so text that appears only once you sign in isn’t there. It doesn’t run the page’s JavaScript either, unless the monitor has Include JavaScript rendering on: then body and JSON rules read the text of the page as the browser shows it, only the first 4 KB of that text, and header rules still read the headers your server sent. See Check a Page as a Browser Shows It.
  • On a plan without response rules, a monitor that already has rules keeps them, but they’re paused: the section marks them Paused, and checks don’t run them, so the monitor passes on its status code alone. You can still change the monitor’s other settings, or press Remove all rules. Adding or changing a rule needs a paid plan. When the plan has response rules again, the kept rules run again, with nothing to change.

These headers tell browsers to protect your visitors. A rule on each catches a deploy or a CDN change that drops one. All but the last are under Quick add; add the last with Add rule.

  • HSTS is present (Strict-Transport-Security): tells browsers to use only HTTPS for your site, so a visitor can’t be quietly moved to an unencrypted connection.
  • Content-Security-Policy is present: lists where the page may load scripts, styles and images from, which blocks most injected scripts.
  • X-Content-Type-Options is nosniff: stops browsers guessing a file’s type, so an uploaded file can’t be run as a script. The rule asks that the header equals nosniff, ignoring case.
  • X-Frame-Options is present: stops other sites showing your page inside theirs to trick visitors into clicking. If your content security policy uses frame-ancestors instead, change the rule to a Content-Security-Policy header that contains frame-ancestors.
  • Referrer-Policy is present: limits how much of your page’s address is sent to the sites your visitors go on to.
  • X-Powered-By is absent: keeps your server from announcing the software it runs, which tells an attacker what to try.
  • Permissions-Policy, checked with is present: turns off browser features your site doesn’t use, such as the camera or location.

Some headers change on every answer, or between the servers of a CDN (a service that keeps copies of your site in many places): Date, Age, cache status headers such as cf-cache-status or x-cache, and request IDs such as cf-ray or x-request-id. A rule that wants one exact value for one of them fails at random and alerts when nothing is wrong. Check only that they’re present, or leave them out.

See A Response Rule Failed.