How can I remove a query parameter from a URL?
I'm trying to remove a query parameter from a URL, and I'm stuck...
Basically, I may have 0-n parameters appended to a querystring. They will all have a similar name, starting with "Context_", e.g. "Context_Site", "Context_Device_Type", "Context_Platform". My original plan was to remove the parameters using the following code (which I got from another question on this forum that I can no longer find.:
when HTTP_REQUEST {
if {[HTTP::query] contains "Context_"} {
set newq ""
Split query string into separate parameters
foreach param [split HTTP::query] "&"] {
Only add non-context query parms into newq
if {!($param starts with "Context_")} {
if {$newq eq ""} {
append newq "?$param"
} else {
append newq "&$param"
}
}
Add new query portion to path
if {$newq ne ""} {
HTTP::uri "[HTTP::path]$newq"
} else {
HTTP::uri [HTTP::path]
}
}
}
}
The thing is, when I try to run this in Tcl for Windows (Tcl 8.5.8, Tk 8.5.8), I get an "invalid bareword" error for the "contains" in the second line:
invalid bareword "contains"
in expression "[HTTP::query] _@_contains "Context_...";
should be "$contains" or "{contains}" or "contains(...)" or ...
I think the syntax is correct, so is this due to a problem with the version of Tcl I'm using?
Also, on a related note (and I can ask this as a separate question if it makes sense), if I know that the Context_ parameter(s) will be the last parameter(s) in the URL, is there a better/faster way to remove them than to parse out the querystring into its individual parameters and check each one in turn? For instance, could I just search for the first instance of "&Context_"in HTTP::query and just strip out everything from that point on (also checking for HTTP:query starts with "Context_", I guess).