Reputation: 1876
Im trying to get response from using GeoNames API. and here my Code
NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];
NSURL *baseURL = [NSURL URLWithString:@"http://api.geonames.org/findNearbyPostalCodesJSON"];
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
[delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
Im getting following error.
Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: forbidden (403)" UserInfo={NSUnderlyingError=0x7ff013d840f0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ff013d07440> { URL: http://api.geonames.org/findNearbyPostalCodesJSON/ }
i tried with hurl.it
to check whether response coming or not. and its coming fine.
Surprise is im using same above code for various other requests with only changing URL and those are working fine.
UPDATED
And previously it worked for the following code. and i did some code quality adjustment and transferred the code to above. then got the problem
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"lat": lat,
@"lng": lon,
@"username" : @"testing"
};
[manager POST:@"http://api.geonames.org/findNearbyPostalCodesJSON" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
}
Upvotes: 1
Views: 494
Reputation: 1876
I found the solution. i even upgraded the AFNetworking to 3.0 from 2.X
and Code Changed like below. Special Changes GET
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager GET:@"http://api.geonames.org/findNearbyPostalCodesJSON" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"success!");
[delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error: %@", error);
}];
Upvotes: 1
Reputation: 1
"Request failed: unacceptable content-type: text/html"
because the AFNetworking only support @"application/json", @"text/json", @"text/javascript"
you should add a @"text/html" type
[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];
NSURL *baseURL = [NSURL URLWithString:@"http://api.geonames.org/findNearbyPostalCodesJSON"];
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//insert this code
[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
[delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
Upvotes: 0
Reputation: 1858
When I just enter the URL http://api.geonames.org/findNearbyPostalCodesJSON into a browser, I get the following JSON:
{"status":{"message":"Please add a username to each call in order for geonames to be able to identify the calling application and count the credits usage.","value":10}}
So I suspect that, at the very least, you need to change your 'POST' to a 'GET':
[manager GET:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
[delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
Beyond that, based on the returned content, it looks like you'll need some additional application-level logic to provide user identification/authentication in order to get the actual data you're interested in, but the above change should at least trigger an invocation of the success block.
Upvotes: 1