Steve Nay
Steve Nay

Reputation: 2829

URI-encoding a string

I need to send a GET request. One of the parameters has URL query characters in it (e.g., ?, &, and =). How do I URI-encode that in KRL before sending the request?

Here's the pseudocoded idea:

params = "key=value&key=value";
encoded_params = params.urlencode();
request = datasource:service("?data=#{encoded_params}");

Upvotes: 2

Views: 147

Answers (2)

Randall Bohn
Randall Bohn

Reputation: 2647

See also http://docs.kynetx.com/docs/URI

escaped = uri:escape("a b c d"); // "a%20b%20c%20d"
original = uri:unescape(escaped); // "a b c d"

Upvotes: 1

TelegramSam
TelegramSam

Reputation: 2790

You can either pass a string or a struct when you call a datasource. When you use a hash, the hash values are URL encoded automagically by the platform.

Your code above would be written like so:

rparams = {
  "key1": "value1",
  "key2": "value2"
};
request = datasource:service(rparams);

TaDa! Magic.

Note that I used string literals in the hash declaration, but those can be any expressions, and the values will be passed as the arguments in the datasource request.

Upvotes: 2

Related Questions