Reputation: 4912
I am trying to form a request using NSURL
.
My code:
(somewhere)
#define CLASS_URL @"https://www.someurl.com/xyz"
(somewhere)
NSString *className = @"className";
Then my main code:
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@&name=\"%@\"", CLASS_URL, className]];
I have read many answers on adding a quote mark on StackOverflow saying what I should use. I have tried :
%22
and %%22
stringByAddingPercentEscapesUsingEncoding
\"
as shown in the presented codeHowever none of the answers seem to be working. I am always getting (null)
when calling NSLog(@"URL: %@", url);
Anyone has any idea on how to do this correctly?
Edit: I tried using stringByAppendingString as suggested, still not working.
NSString *tmp = [CLASS_URL stringByAppendingString:[NSString stringWithFormat:@"&name=\"%@\"",className]];
Expected result:
www.someurl.com/xyz&name="className"
I need double quotes in case user types a space.
Upvotes: 1
Views: 1016
Reputation: 112855
The quotes in the URL need to be replaces with escaped quotes:
#define CLASS_URL @"https://www.someurl.com/xyz"
NSString *className = @"className";
NSString *query = [NSString stringWithFormat:@"name=\"%@\"", className];
// URL encode the query
query = [query stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *urlString = [NSString stringWithFormat:@"%@?%@", CLASS_URL, query];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"URL: %@", url);
Also the "&" needs to be replaced with ?
, query strings begin with a ?
and subsequent arguments are separated with '&'.
Upvotes: 1
Reputation: 299465
Your expected URL is incorrect.
I need double quotes in case user types a space.
Double-quoting does not make spaces legal in an URL. Spaces are reserved, and must be percent encoded, in quotes or not. Double-quotes are not part of the unreserved space, so should be quoted as well, if you needed them (but that wouldn't save you from encoding spaces).
The way to build this is to encode the string you want to send:
NSString *className = @"className with space";
NSString *quotedClassName = [className stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *urlString = [NSString stringWithFormat:@"%@?name=%@", CLASS_URL, quotedClassName];
NSURL *url = [[NSURL alloc] initWithString:urlString];
This will encode to:
https://www.someurl.com/xyz?name=className%20with%20space
Notice I've removed the double-quotes entirely. If you really need them, then you can get them by making your original string include them, and then encode them:
NSString *className = @"\"className with space\"";
NSString *quotedClassName = [className stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
This will encode to:
https://www.someurl.com/xyz?name=%22className%20with%20space%22
(I've also fixed your query parameter, which might just be a typo. The query is separated from the path by ?
, not &
.)
Upvotes: 7