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 encodevarname

uri component-encodevarname

uri aws-object-encodevarname

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 encodevarname

This method will encode all characters except for the following:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~
: / ? # [ ] @ ! $ & ' ( ) * + , ; =

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-componentvarname

This method will encode all characters except for the following:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~

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-encodevarname

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 hierarchical filesystem.

The 'files' in buckets are termed objects and to assist in organizing 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:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~ /

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

var name = "example/name"
var domain = "example@domain.com"

uri encode-component name
uri encode-component domain

var URL = "http://server.com/resource?name=${name}&domain=${domain}
print URL is now: ${URL}

The above script will output:

URL is now: http://server.com/resource?name=example%2Fname&domain=example%40domain.com

Last updated