URLComponents queryItems losing percent encoding when mutated
You should use percentEncodedQuery
if you have a query that is already percent encoded:
let startURL = "https://test.com/test.jpg"
var components = URLComponents(string: startURL)!
components.percentEncodedQuery = "X-Test-Token=FQdzEPH%2F%2F%2F"
if let compURL = components.url {
print(compURL)
}
Or you can specify it unescaped (and it leaves it unescaped as it's not necessary to escape /
characters in a query):
let startURL = "https://test.com/test.jpg"
var components = URLComponents(string: startURL)!
components.queryItems = [URLQueryItem(name: "X-Test-Token", value: "FQdzEPH///")]
if let compURL = components.url {
print(compURL)
}
And if you have to update queryItems
, just make sure to set percentEncodedQuery
at the very end:
let startURL = "https://test.com/test.jpg"
let encodedQuery = "X-Test-Token=FQdzEPH%2F%2F%2F"
var components = URLComponents(string: startURL)!
components.queryItems = [URLQueryItem(name: "foo", value: "bar, baz, & qux")]
if let query = components.percentEncodedQuery {
components.percentEncodedQuery = query + "&" + encodedQuery
} else {
components.percentEncodedQuery = encodedQuery
}
if let compURL = components.url {
print(compURL)
}
Encode '+' using URLComponents in Swift
As pointed out in the other answers, the "+" character is valid in
a query string, this is also stated in thequeryItems
documentation:
According to RFC 3986, the plus sign is a valid character within a query, and doesn't need to be percent-encoded. However, according to the W3C recommendations for URI addressing, the plus sign is reserved as shorthand notation for a space within a query string (for example,
?greeting=hello+world
).
[...]
Depending on the implementation receiving this URL, you may need to preemptively percent-encode the plus sign character.
And the
W3C recommendations for URI addressing
state that
Within the query string, the plus sign is reserved as shorthand notation for a space. Therefore, real plus signs must be encoded. This method was used to make query URIs easier to pass in systems which did not allow spaces.
This can be achieved by "manually" building
the percent encoded query string, using a custom character set:
let queryParams = ["foo":"a+b", "bar": "a-b", "baz": "a b"]
var components = URLComponents()
var cs = CharacterSet.urlQueryAllowed
cs.remove("+")
components.scheme = "http"
components.host = "www.example.com"
components.path = "/somepath"
components.percentEncodedQuery = queryParams.map {
$0.addingPercentEncoding(withAllowedCharacters: cs)!
+ "=" + $1.addingPercentEncoding(withAllowedCharacters: cs)!
}.joined(separator: "&")
let finalURL = components.url
// http://www.example.com/somepath?bar=a-b&baz=a%20b&foo=a%2Bb
Another option is to "post-encode" the plus character in the generated
percent-encoded query string:
let queryParams = ["foo":"a+b", "bar": "a-b", "baz": "a b"]
var components = URLComponents()
components.scheme = "http"
components.host = "www.example.com"
components.path = "/somepath"
components.queryItems = queryParams.map { URLQueryItem(name: $0, value: $1) }
components.percentEncodedQuery = components.percentEncodedQuery?
.replacingOccurrences(of: "+", with: "%2B")
let finalURL = components.url
print(finalURL!)
// http://www.example.com/somepath?bar=a-b&baz=a%20b&foo=a%2Bb
iOS: Prevent URL from being percent escaped automatically?
Per the spec for URI: "A host identified by an Internet Protocol literal address, version 6 [RFC3513] or later, is distinguished by enclosing the IP literal within square brackets ("[" and "]")." rfc3986.
Thus you can't use square brackets for your purpose without escaping them. Your server's REST service is at fault for not handling the escaped characters in the query params. (And I've had situations in recent past where I've had to ask my REST team to fix this sort of problem where they forgot to support escaped query parameter values).
Swift URLComponents not converting empty spaces correctly
So the answer is that NSLog
does not print out percent Escaped strings correctly, print
on the other hand does. The best thing would have been just to set a breakpoint on the NSLog
line and check the url value if it is correct, which it was.
How do I URL encode a string
Unfortunately, stringByAddingPercentEscapesUsingEncoding
doesn't always work 100%. It encodes non-URL characters but leaves the reserved characters (like slash /
and ampersand &
) alone. Apparently this is a bug that Apple is aware of, but since they have not fixed it yet, I have been using this category to url-encode a string:
@implementation NSString (NSString_Extended)
- (NSString *)urlencode {
NSMutableString *output = [NSMutableString string];
const unsigned char *source = (const unsigned char *)[self UTF8String];
int sourceLen = strlen((const char *)source);
for (int i = 0; i < sourceLen; ++i) {
const unsigned char thisChar = source[i];
if (thisChar == ' '){
[output appendString:@"+"];
} else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
(thisChar >= 'a' && thisChar <= 'z') ||
(thisChar >= 'A' && thisChar <= 'Z') ||
(thisChar >= '0' && thisChar <= '9')) {
[output appendFormat:@"%c", thisChar];
} else {
[output appendFormat:@"%%%02X", thisChar];
}
}
return output;
}
Used like this:
NSString *urlEncodedString = [@"SOME_URL_GOES_HERE" urlencode];
// Or, with an already existing string:
NSString *someUrlString = @"someURL";
NSString *encodedUrlStr = [someUrlString urlencode];
This also works:
NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );
Some good reading about the subject:
Objective-c iPhone percent encode a string?
Objective-C and Swift URL encoding
http://cybersam.com/programming/proper-url-percent-encoding-in-ios
https://devforums.apple.com/message/15674#15674
http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/
Related Topics
Spritekit and Swiftui, Change Scene a Better Way
Remove Add Calendar Button from Calendar Chooser
Realitykit - Keep Object Always in Front of Screen
Difference Between Any? and Any
Swiftui Concatenate Multiline Tappable Text
Optional Chaining and Array in Swift
I Am Loading Videos in Avplayer in Collection View But It Repeats Some Cells Data
Geofire/Firebase Function Is Executing Handler Multiple Times in Swift
Pie Chart Entries Outside Slices Have Different Position Offsets
Map Dictionary Keys to Add Values - Swift
Command Center Scrubber on Lock Screen Swift
How Do We Use of Nsselectorfromstring in Swift
Calculating Distance in Vapor4