views:

96

answers:

1

I keep getting: Unable to create request (bad url?) error message on a request like:


- (void)grabURLInBackground :(id)sender 
{
    NSURL *url= [NSURL URLWithString:@"http://api.website.com/api_requests/standard_request/getItemRequest={\"Id\":\"Logic\"}"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request addRequestHeader:@"X-WEBSITE-API-DEV-NAME" value:@"cghjm"];
        [request setDelegate:self];
    [request startAsynchronous];
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];
   NSLog(responseString);
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
    NSLog(@"%@:%s Error: %@", [self class], _cmd, [error localizedDescription]);
}

I'm "VERY" new to Objective-C...

Thanks

-Brad

+1  A: 

Change -startAsynchronous to -startSynchronous and remove the line where you set the request's delegate to self. Asynchronous means that it runs in the background, in which case you need to use the delegate methods. Since you've put everything in one method, you want to use a synchronous request.

Edit

Initialize the URL like this:

[NSURL URLwithString: [@"http://..." stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]]
Alexsander Akers
I tried it both ways... I actually have the requestFinished and requestFailed functions also... I am updating the code to show what I've got exactly since I forgot to include that part.
Brad
I think the problem has to do with the {} in the URL.. although I've not had a problem with other languages, php, delphi...
Brad
I just tried that, it still doesn't work..
Brad
It's a problem with the website requiring the curly braces, and double quotes in the URL (JSON) and NSUrl not allowing those characters to be in the URL. Thank you for your help, I was able to verify the problem and request an update to the API to fix the issue. stringByAddingPercentEscapesUsingEncoding is what I needed to add.
Brad
What worked for me was this: http://stackoverflow.com/questions/705448/iphone-sdk-problem-with-ampersand-in-the-url-string/706057#706057 - Roger manually replaced the problematic characters within string.
Merlin