http_status_matches

BOOLhttp_status_matchesINTEGERstatusSTRINGfmt

Available in all subroutines.

Determines whether the HTTP status matches or does not match any of the statuses in the supplied fmt string.

Returns true when the status matches any of the strings and returns false otherwise. If fmt is prefixed with !, returns true when the status does not match any of the strings and returns false if it does. Statuses in the string are separated by commas.

This function is not prefixed with the std. namespace.

Example

The following snippet in vcl_fetch marks backend responses with status codes other than 200, 301, or 302 as not cacheable.

if (http_status_matches(beresp.status, "!200,301,302")) {
set beresp.cacheable = false;
}

Try it out

http_status_matches is used in the following solution recipes, which show real world use cases. Click RUN to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Replace origin errors with 'safe' responses

If origin responds with 500 internal server error, modify status to 503 and serve a 'safe' error message.

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.