1
Code:
     arrValues = [[NSMutableArray alloc]initWithObjects:@"Chennai", nil];
            arrKeys = [[NSMutableArray alloc]initWithObjects:@"loc", nil];
            dicValue = [NSDictionary dictionaryWithObjects:arrValues forKeys:arrKeys];
             NSString *strMethodName = @"agentuserlist";

            strUrlName = [NSString stringWithFormat:@"%@%@?filters=%@",appDelegate.strURL, strMethodName, dicValue];
            //strUrlName = [strUrlName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];


        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:strUrlName] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:250.0];
        [request setHTTPMethod:@"GET"];
        [request setHTTPShouldHandleCookies:YES];

        [request setValue:@"zyt45HuJ70oPpWl7" forHTTPHeaderField:@"Authorization"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

        NSError *error;
        NSURLResponse *response;
        NSData *received = [NSURLConnection sendSynchronousRequest:request
                                                 returningResponse:&response error:&error];
        NSLog(@"error:%@", error);

        NSString *strResponse = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
        NSLog(@"response :%@", strResponse);

I try to send json argument through get method.It gives error as "unsupported url(-1002)".

The URL is working fine when I checked with Postman. I am unable to find out the problem.

Where I went wrong?

user3823935
  • 891
  • 2
  • 16
  • 26
  • Can you write out the content of `request.absoluteString`? Do you get the error when creating the `NSURL` instance or when trying to send the request? – Romain Apr 02 '15 at 04:51
  • i am getting response as Unsupported URL – user3823935 Apr 02 '15 at 04:53
  • do u want entire URL? – user3823935 Apr 02 '15 at 04:58
  • just log your strUrlName, I guess its not correct! – Retro Apr 02 '15 at 05:27
  • 2
    The building of the URL like this is simply incorrect. You can't just `stringWithFormat` a dictionary into a URL (even if you percent escaped it) and expect it to work. You must first confirm what the web service requires, and only then can you go about building a request. I suspect that something like Romain's answer is likely, but don't know where this `filters=` falls into the mix (I presume you picked that up from somewhere in the documentation). But it's silly for us to try to tell you how to fix this without clear guidance on what the web service expects. – Rob Apr 02 '15 at 05:29
  • (By the way, when you get this current problem behind you, you really should retire the use of synchronous requests.) – Rob Apr 02 '15 at 05:30

1 Answers1

3

I think the problem lies in how you encode your NSDictionary in the NSURL.

You probably want your URL to look like this: http://my domain.com/agentuserlist?loc=Chennai. But the raw encoding of the NSDictionary inside the NSURL doesn't produce this result.

You can follow the accepted answer from this question to get an idea of how to transform an NSDictionary into a regular list of URL parameters (with proper encoding of dictionary values: don't forget the stringByAddingPercentEscapeUsingEncoding part): Creating URL query parameters from NSDictionary objects in ObjectiveC

Community
  • 1
  • 1
Romain
  • 3,718
  • 3
  • 32
  • 48