Sometimes you want to transform a string value into a URL encoded value, so it can be used as part of a valid URL.
For example a string with spaces or special characters can not be used in a URL as is, but needs to be URL encoded.
Nushell has the url encode
command to achieve this.
You can simple run this command on a string value and the result is a URL encoded value.
With the option --all
or -a
even more special characters like a dot (.
) are encoded.
The input of the command can be a string value or a list of string values.
But it is also possible to use a record or table structure, but then you need to add as extra argument the name or names of the keys or columns of which the string values should be encoded.
Oppossed to URL encoding a value you can also decode a URL encoded value using the url decode
command.
This command doesn’t have a special option to run.
Just like with the url encode
command the url decode
command works on strings, list of strings, records and tables.
If the input is a record or table the name of key or column of which the values must be decoded must be passed as extra arguments.
In the following example script the url encode
command is used with several input types:
use std/assert # Using url encode command to special characters with # percent-encoded values. For example a space becomes %20. assert equal ('Nushell rocks.' | url encode) 'Nushell%20rocks.' # Using the option --all (or -a) to encoding also special characters # like ; and . assert equal ('Nushell rocks/is awesome.' | url encode --all) 'Nushell%20rocks%2Fis%20awesome%2E' # The url encode command can be used on list of string values and # each value in the list will be encoded. (assert equal (['Nushell rocks' 'Nushell is awesome'] | url encode) ['Nushell%20rocks' 'Nushell%20is%20awesome']) # The url encode command can be used on a record, # but then as extra argument the name of the key(s) # must be given. The value of the key will be encoded. (assert equal ({message: 'Nushell rocks', type: 'simple'} | url encode message type) {message: 'Nushell%20rocks', type: 'simple'}) # Also for a table you can use the url encode command. # An extra argument is needed to specify the column names # of which the values should be encoded. let table = [ [message private]; ['Nushell rocks!' true] ['is awesome' true] ] (assert equal ($table | url encode --all message) [ [message private]; ['Nushell%20rocks%21' true] ['is%20awesome' true] ])
In the following examples you can see the url decode
command used:
use std/assert # Using url encode command to special characters with # percent-encoded values. For example a space becomes %20. assert equal ('Nushell%20rocks.' | url decode) 'Nushell rocks.' # Using the option --all (or -a) to encoding also special characters # like ; and . assert equal ('Nushell%20rocks%2Fis%20awesome%2E' | url decode) 'Nushell rocks/is awesome.' # The url encode command can be used on list of string values and # each value in the list will be encoded. (assert equal (['Nushell%20rocks' 'Nushell%20is%20awesome'] | url decode) ['Nushell rocks' 'Nushell is awesome']) # The url encode command can be used on a record, # but then as extra argument the name of the key(s) # must be given. The value of the key will be encoded. (assert equal ({message: 'Nushell%20rocks', type: 'simple'} | url decode message type) {message: 'Nushell rocks', type: 'simple'}) # Also for a table you can use the url encode command. # An extra argument is needed to specify the column names # of which the values should be encoded. let table = [ [message private]; ['Nushell%20rocks%21' true] ['is%20awesome' true] ] (assert equal ($table | url decode message) [ [message private]; ['Nushell rocks!' true] ['is awesome' true] ]) # Running encode and decode should return the original string. assert equal ('Nushell rocks!' | url encode | url decode) 'Nushell rocks!'
Written with Nushell 0.107.0.