WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

1      

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.)

1      

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

1      

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

1      

Thank you very much for you support!

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.