Reputation: 12884
I send a HTTP GET to the Google search API with RESTKit:
http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Sulgen
This URL works fine when beeing used with NSURLRequest.
RESTKit transforms this string to
http://maps.googleapis.com/maps/api/geocode/json/?sensor=false&address=Sulgen
Hint: a slash is inserted in the string before
I used the GET Method to fire the request. How is this supposed to work?
EDIT: here is the code that leads to the above behavior:
+ (void)loadGoogleMapsApiLocation_Restkit:(NSString*)searchString
{
/// String definitions
NSString *requestString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Frankfurt"];
NSLog(@"requestString: %@",requestString);
/// Create Mappers
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[GooglePlacemark class]];
[objectMapping addAttributeMappingsFromArray:
@[@"formatted_address"]];
/// setup responseDesriptor
NSString *key_path = @"results";
/// use this new Restkit method instead of the deprecated one
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:key_path
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
/// Create Manager, connect responseDescriptor and execute
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:requestString]];
[manager addResponseDescriptor:responseDescriptor];
[manager getObject:nil
path:@""
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
NSLog (@"sucess!");
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
NSLog (@"Error: %@", error);
NSLog (@"Response: %@", operation.HTTPRequestOperation.responseString);
}
];
}
The error messages are these:
I restkit.network:RKObjectRequestOperation.m:150 GET 'http://maps.googleapis.com/maps/api/geocode/json/?sensor=false&address=Frankfurt'
E restkit.network:RKObjectRequestOperation.m:547 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 404" UserInfo=0xafaff00 {NSLocalizedRecoverySuggestion=<!DOCTYPE html>
The 404 message comes because Restkit inserted the backslash into the request String. The original string request works fine if fired in a browser or rest client.
Any help appreciated.
Upvotes: 1
Views: 1227
Reputation: 119031
You need to reorganise your base URL and paths:
NSString *baseURLString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode"];
NSString *requestPath = @"json";
NSDictionary *parameters = @{@"sensor":@"false",@"address":@"Frankfurt"};
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseURLString]];
[manager getObject:nil
path:requestPath
parameters:parameters
...
Upvotes: 2