querystring.get

STRINGquerystring.getSTRINGurlSTRINGname

Available inall subroutines.

Returns a value associated with the name in the query component of an URL.

If the query component does not contain a value associated with name, then querystring.get returns a not set value. For example, querystring.get("/?a=1&b=2&c=3", "d") is a not set value.

If a query parameter associated with name is found but the value is absent, then querystring.get returns the string "". For example, querystring.get("/?a=1&b=&c=3", "b") is "".

If multiple query parameters of the same name are present in the query component, then querystring.get returns the first value associated with name. For example, querystring.get("/?a=1&b=2&c=3&b=4&d=5", "b") is "2".

If the URL does not include a query component, then querystring.get returns a not set value. For example, querystring.get("/", "a") is a not set value.

If querystring.get is called with not set or empty string arguments, then it returns a not set value. For example, querystring.get("", "") is a not set value.

To check if the return value is not an empty string or a not set value, use the std.strlen function, which returns 0 in both cases.

if (std.strlen(querystring.get(req.url, "foo")) > 0) {
// Do something if the value associated with "foo" is not an empty string
// or a not set value.
}

This function conforms to the URL Living Standard.

Examples

querystring.get("/?foo=", "foo") # returns ""
querystring.get("", "") # returns a not set value
querystring.get("/?foo=&foo=bar", "foo") # returns ""
querystring.get("/?a=1&b=2&c=3&d=4", "b") # returns "2"
querystring.get("/?a=1", "b") # returns a not set value
querystring.get("/?foo", "foo") # returns a not set value
querystring.get("/?a=1&b=2&c=3&d=4&b=5", "b") # returns "2"
querystring.get(not set string, not set string) # returns a not set value

User contributed notes

BETA

Do you see an error in this page? Do have an interesting use case, example or edge case people should know about? Share your knowledge and help people who are reading this page! (Comments are moderated; for support, please contact support@fastly.com)