Reputation: 87
I'm trying to call a drupal search webservice, but when I call it, it says that the request is null.
I put some log in my code and saw that the NSString was good but when I define my NSURL, the NSString is seen as null... Can somebody explain me what is the problem please ?
Following is my code :
- (void)FindProductsByKeyword:(NSString *)keyword :(void(^)(NSArray *produits))success
{
__block NSArray *produits = [[NSArray alloc]init];
NSString *fullUrl = [NSString stringWithFormat:@"%@%@?filter[title]=\%%%@\%%&filter_op[title]=LIKE", WEBSERVICES_URL, PRODUCT_DISPLAY_URL, keyword];
NSLog(@"Appel de : %@", fullUrl);
NSURL *URLFormatted = [NSURL URLWithString:fullUrl];
NSLog(@"Appel de : %@", URLFormatted);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:fullUrl]];
NSLog(@"Appel de : %@", request);
[request setHTTPShouldHandleCookies:NO];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:
^(NSURLResponse * response, NSData * data, NSError * error)
{
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"status code : %d", httpResponse.statusCode);
if(httpResponse.statusCode == 200) {
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
produits = [[WebservicesService getInstance]ExtractListProduct:jsonData];
NSLog(@"Il y a %d résultats pour la recherche", produits.count);
}
success(produits);
}];
}
This is what my log show :
2014-01-03 17:03:51.852 mcommerce_ipad[8649:a0b] Appel de : http://domain-name.com/rest/product-display.json?filter[title]=%test%&filter_op[title]=LIKE
2014-01-03 17:03:51.852 mcommerce_ipad[8649:a0b] Appel de : (null)
2014-01-03 17:03:51.853 mcommerce_ipad[8649:a0b] Appel de : <NSMutableURLRequest: 0x8a3c9c0> { URL: (null) }
2014-01-03 17:03:51.895 mcommerce_ipad[8649:a0b] status code : 0
Finally what firebug return when I try directly my NSString.
200 OK
17 Object { vid="17", uid="0", title="Ethylotest chimique à usage unique", more...}
139 Object { vid="139", uid="16", title="Produit de test", more...}
142 Object { vid="142", uid="16", title="Test surface", more...}
This show that the request is correct and should return something, the problem comes from the convertion from NSString to NSURL but I can't find it... So please be my saviours !
Upvotes: 0
Views: 569
Reputation: 6557
As I've said in my comment, you need to escape special characters or URLWithString will return null.
To do so, use:
[NSURL URLWithString:[fullUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
Upvotes: 1
Reputation: 119031
You need to URL encode the parameters section of the string before converting it to a URL. It works in a browser because the browser does it for you before sending the request.
Specifically, you need to modify the %
symbol.
Upvotes: 1