TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Create URL with URLComponents - QueryItem containing "%" problem

Forums > iOS

I have a problem when query item has a symbol %: It always adds "25" after the % symbol

var components = URLComponents(string:"https://gmail.com")!
components.queryItems = [URLQueryItem(name: "k%3", value: "val")]

print(components.queryItems!)  // Output: [k%3=val]

print(components.url!) // Output: https://gmail.com?k%253=val

from this example: How can I get https://gmail.com?k%3=val ??

Thank you in advance

3      

Are you sure the query parameter key you are working with is k%3?

String has the property removingPercentEncoding that converts such sequences into their original characters. But if the string doesn't contain any valid percent-encoded characters it will return nil.

"https://gmail.com?k%3=val".removingPercentEncoding returns nil.

Not only that, but percent-encoded values are hex values and have 2 digits. %3 is not listed a valid value in any reference I can find. For example, %3A is : and is probably the most commonly seen code.

So, I guess what I'm saying is to check your inputs to make sure k%3 is correct.

(And, if it is actually correct, I'm curious what k%3 signifies in a gmail URL.)

3      

Thanks for the reply.

I'm quite new in swift and in web and did not know anything about percent-encoded values. Thanks for that.

Actually, I'm not working with gmail, and key is not k%3. I just wrote that as an example. In reality, my parameter key is "filters%5Bstudio%5D". However, when I check my url, its - filters%255Bstudio%255D

I will try to get some info about percent-encoded values

3      

Actually, I'm not working with gmail, and key is not k%3. I just wrote that as an example. In reality, my parameter key is "filters%5Bstudio%5D". However, when I check my url, its - filters%255Bstudio%255D

Ah, okay. %25 is the encoding for %. So it's encoding the % sign that is part of your already encoded parameter.

Since %5B and %5D are the codes for [ and ], you will need to use a name key like this "filters[studio]" in order for the URL to come out correct.

var components = URLComponents(string:"https://gmail.com")!
components.queryItems = [URLQueryItem(name: "filters[studio]", value: "val")]

print(components.queryItems!) // output: [filters[studio]=val]

print(components.url!)  // output: https://gmail.com?filters%5Bstudio%5D=val

3      

Thank you very much for you support!

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.