uri
The uri
statement is used to encode the contents of a variable such that it does not contain any illegal or ambiguous characters when used in an HTTP request.
Syntax
uri encode
varname
uri component-encode
varname
uri aws-object-encode
varname
As well as uri component-encode
you can use uri encode-component
(the two are identical in operation). Similarly, uri aws-object-encode
and aws-encode-object
are aliases for each other.
Details
When sending a request to an HTTP server it is necessary to encode certain characters such that the server can accurately determine their meaning in context. The encoding involves replacing those characters with a percent symbol - %
- followed by two hexadecimal digits representing the ASCII value of that character.
Note that the last parameter to the uri
statement is a variable name, so to encode the contents of a variable called my_query
the correct statement would be uri encode my_query
and not uri encode ${my_query}
(The latter would only be correct if the value of my_query
was the name of the actual variable to encode)
USE script provides the following methods for encoding the contents of a variable:
encode
uri encode
varname
This method will encode all characters except for the following:
This is typically used to encode a URI which contains spaces (spaces encode to %20
) but doesn't contain any query parameters.
encode-component
uri encode-component
varname
This method will encode all characters except for the following:
This is typically used to encode query components of a URI, such as usernames and other parameters. Note that this method will encode the symbols =
, &
and ?
and as such a URL of the form:
server.com/resource?name=
name_value
&domain=
domain_value
is usually constructed from its various components using the values of the parameters as shown in the example below.
aws-object-encode
uri aws-object-encode
varname
This method is specifically implemented to support the encoding of object names when downloading from Amazon S3 buckets. Amazon S3 buckets appear much like shared directories, but they do not have a heirarchical filesystem.
The 'files' in buckets are termed objects and to assist in organising the contents of a bucket, object prefixes may be used to logically group objects together.
These prefixes may include the forward slash character, making the resulting object name appear identical to a conventional pathname (an example might be billing_data/20180116_usage.csv
). When downloading an object from S3 the object name is provided as part of the HTTP query string.
When referencing an S3 object name there is an explicit requirement not to encode any forward slashes in the object name. USE therefore provides the aws-object-encode
method to ensure that any S3 object names are correctly encoded. This method will encode all characters except for the following:
More information may be found at https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html where it states:
URI encode every byte. UriEncode() must enforce the following rules:
URI encode every byte except the unreserved characters: 'A'-'Z', 'a'-'z', '0'-'9', '-', '.', '', and '~'._
The space character is a reserved character and must be encoded as "%20" (and not as "+").
Each URI encoded byte is formed by a '%' and the two-digit hexadecimal value of the byte.
Letters in the hexadecimal value must be uppercase, for example "%1A".
Encode the forward slash character, '/', everywhere except in the object key name. For example, if the object key name is photos/Jan/sample.jpg, the forward slash in the key name is not encoded.
The usr-object-encode
method is compliant with the above requirements. For most trivial cases it should not be necessary to encode the AWS object name as it is relatively straightforward to do it by hand. However using uri aws-object-encode
to URI-encode the object name may be useful for object names that contain a number of characters not listed above or for cases where the object name is provided as a parameter to the USE script.
Example
The above script will output:
Last updated