Uri.BuildQueryString is a Power Query M function that assembles a record query into a URI query string, escaping characters as necessary. The function returns a properly formatted and escaped URI query string based on the input record.
Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365
Syntax
Uri.BuildQueryString( query as record ) as text
Description
The Uri.BuildQueryString function encodes a record (a collection of key-value pairs) into a URL-encoded query string. This is useful for constructing URLs with parameters, especially when special characters and spaces must be represented in a way that adheres to URL encoding standards.
Examples
Encode a query string which contains some special characters.
// Output: "a=1&b=%2B%24"
Uri.BuildQueryString( [a = "1", b = "+$"] )
Suppose you want to create an authorization URL to connect to the Discord API. Here’s a simplified example of how this logic might look:
let
authorize_uri = "https://discord.com/api/oauth2/authorize?",
myRecord =
[
client_id = "111222333",
redirect_uri = "http://localhost/discord/redirect",
response_type = "code",
scope = "identify email connections guilds guilds.members. read"
],
// Generates: "client_id=111222333&redirect_uri=http%3A%2F%2Flocalhost%2Fdiscord%2Fredirect&response_type=code&scope=identify%20email%20connections%20guilds%20guilds.members.%0D%0A%20%20%20%20%20%20%20%20read"
queryString = Uri.BuildQueryString( myRecord ),
AuthorizeURL = authorize_uri & queryString
in
AuthorizeURL
The Uri.BuildQueryString function takes the record myRecord as input and converts it into a properly encoded query string that can be appended to a URL. So how does that work?
- Input (myRecord):
myRecordis a collection of key-value pairs that represent the parameters required for the authorization URL: - Output (queryString): The
Uri.BuildQueryStringfunction takes the record (myRecord) and converts it into a properly encoded query string. For example:- Special characters like
/inredirect_uriare encoded as%2F. - Spaces in
scopeare encoded as%20.
- Special characters like
- Final URL (AuthorizeURL): The base URL (
authorize_uri) is combined with the encoded query string (queryString) to produce the final authorization URL: “https://discord.com/api/oauth2/authorize?client_id=111222333&redirect_uri=http%3A%2F%2Flocalhost%2Fdiscord%2Fredirect&response_type=code&scope=identify%20email%20connections%20guilds%20guilds.members.read”
This URL can be used to initiate the OAuth2 authorization flow for your application.
Related functions
Other functions related to Uri.BuildQueryString are:
2023-2026 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy