How do i specify the query component of a URL?

Suppose i have a URL like:

NSString *urlString = @"https://mail.google.com";

And i want to specify the query component of this URL, so i did this:

NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
[components setQuery:@"guid=56d962c7e4b0b00655a669a2"];
NSLog(@"%@",urlString);
NSLog(@"%@", components.query);

The first log displays https://mail.google.com which is not what i want, however the second log displays guid=56d962c7e4b0b00655a669a2. I don't understand the output of first log, if the second log displays that the query component was added, why does the first log not print it ? What i actually want to do is make a NSMutableRequest to the URL https://mail.google.com?guid=56d962c7e4b0b00655a669a2 but when i do that after the above code and log the result of NSMutableRequest, it says that the required parameter is missing. How do i proceed about this issue. Any help will be appreciated. Thank you.

Answers


If you want to get the URL from the NSURLComponents, use the "URL" property of NSURLComponents.

NSString *urlString = @"https://mail.google.com";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
[components setQuery:@"guid=56d962c7e4b0b00655a669a2"];
NSLog(@"%@", components.URL); // Should log: "https://mail.google.com?guid=56d962c7e4b0b00655a669a2"
NSLog(@"%@", components.query);

NSURL *url = components.URL;

Alternatively, you could just append your queryString onto the end of your string and then turn the whole thing into an NSURL:

NSString *queryString = @"?guid=56d962c7e4b0b00655a669a2";
NSString *urlString = [@"https://mail.google.com" stringByAppendingString:queryString];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"%@", url.absoluteString);

Why don't you try it this way?

NSString *query = @"guid=56d962c7e4b0b00655a669a2"
NSString *urlString = [NSString stringWithFormat:@"https://mail.google.com?%@",query];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

So every time you want to change the query, you can change the query string and then the request will change too.


Need Your Help

Can I use jtracert to sequence diagram a unit test running in eclipse?

eclipse junit sequence-diagram jtracert

How would I go about using jtracert to sequence diagram a junit test being run within eclipse?

Obtain a QImage or QPixmap from QStandardItemModel - TableView

c++ qt

I wanted to know if there was a way of obtaining an image displayed on cell on a table in a way like this.Does QStandardItemModel have any methods that could assist ?