Search code examples
iosobjective-cgoogle-places-apiafnetworking-2

why AFNetworking get unsupported url when putting '|' pipe sign in the url


I've been using AFNetworking for a while and ran into a bizarre issue today. I'm do a GET request using AFNetworking with Google Places API

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=21.283974,-157.836222&radius=1600&types=food|bar|cafe&key=myownapikeyhere

the places nearby search api uses type restriction to constrain the result you can get from the api. see document

the way you do it is to have parameter types=type1|type2|type3|etc, the type1, type2, type3 are the types of places you want fetch with the url. I managed to get results when I paste the url into browser and request it. but whenever I use it with AFNetworking, the '|' sign seems to break it. It throws an unsupported url error.

Is there a reason with this issue? Any suggestion would be helpful.

Thanks!


Solution

  • Are you building this URL yourself? If you used the parameters of a AFNetworking GET request, I believe it would percent escape it properly. But if you build the URL yourself, you're not letting AFNetworking do the necessary percent escaping.

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSDictionary *params = @{@"location" : @"21.283974,-157.836222",
                             @"radius"   : @1600,
                             @"types"    : @"food|bar|cafe",
                             @"key"      : @"myownapikeyhere"};
    
    [manager GET:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"responseObject = %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error = %@", error);
    }];