3

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

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Brad
  • 2,237
  • 5
  • 41
  • 69

1 Answers1

4

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
  • 15,967
  • 12
  • 58
  • 83
  • 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 Sep 18 '10 at 21:23
  • 1
    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 Sep 18 '10 at 21:35
  • 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 Sep 18 '10 at 23:05
  • 2
    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. – kolinko Oct 13 '10 at 05:47