Search

Dark theme | Light theme

November 1, 2025

Nushell Niceties: Create Query Parameters For URL

The build-in HTTP client in Nushell can be used to interact with REST APIs and websites. If the URL you want to invoke has query parameters than you can use the url build-query command. The url build-query command transforms a record or table to URL encoded key/value pairs joined with an ampersand (&). Each key and value is separated by an equal sign (=). The command can expand a key with a list value to separate key/value pairs with the same key if the key is defined in a record.

In the following example url build-query is used to create query parameters for a URL:

use std/assert

# Using url build-query to convert record to valid
# query parameters that can be used in a URL.
(assert equal ({items: 20, page: 1, order_by: name, direction: "asc"} | url build-query)
			 "items=20&page=1&order_by=name&direction=asc")

# Values are automatically URL encoded with url build-query.
(assert equal ({name: 'Hubert Klein Ikkink', fav_candy: 'M&M'} | url build-query)
             'name=Hubert+Klein+Ikkink&fav_candy=M%26M')

# Key with a list value is automatically expanded.
(assert equal ({order_by: [name, age]} | url build-query)
              "order_by=name&order_by=age")

# Instead of a record a table can be used as input.
(assert equal ([[key value];
				[items 20]
				[page 1]
				# Value cannot be a list, like with records.
				# So order_by is defined with two rows.
			    [order_by name]
			    [order_by age]
			    [direction desc]] | url build-query)
              "items=20&page=1&order_by=name&order_by=age&direction=desc")


# Using url build-query with string interpolation to create a URL
# query parameters.
(assert equal $"https://www.mrhaki.com/blog?({query: 'Nushell Niceties'} | url build-query)"
              "https://www.mrhaki.com/blog?query=Nushell+Niceties")

# Alternative to set query parameters in URL
# following https://github.com/nushell/nushell/discussions/15427.
# First parse a string with a URL to a record with url parse,
# update the params key with query parameters
# and transform to URL again with url join.
(assert equal ("https://www.mrhaki.com" | url parse | update params {query: 'Nushell Niceties'} | url join)
			  "https://www.mrhaki.com/?query=Nushell+Niceties")

Written with Nushell 0.108.0.