Reputation: 1177
I am building a simple GET function that should return a JSON object. Working with AFNetworking 2. The code compiles, but i can not pinpoint the source of the error i get during runtime. Could it be that the content-type: on the server is text/html and I expect jsonobj. The server uses "Basic user:id" header, should I add this to setValue forHttpHeaderField?
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://the website.com"]];
manager.securityPolicy.allowInvalidCertificates = YES;
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"pass"];
[manager GET:@"/api/data/interval?month" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@" Yep %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@" Nope %@", error);
}];
And here is the error that I get
Nope Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: forbidden (403)" UserInfo=0x8a9dec0 {NSErrorFailingURLKey=http://thewebsite.com/api/data/interval?month, NSLocalizedDescription=Request failed: forbidden (403), NSUnderlyingError=0x8a99e00 "Request failed: unacceptable content-type: text/html", AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8bb0ac0> { URL: http://thewebsite.com/api/data/interval?month } { status code: 403, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Language" = "";
"Content-Type" = "text/html";
Date = "Tue, 04 Feb 2014 13:02:12 GMT";
Upvotes: 0
Views: 1775
Reputation: 459
First you have to init your base url.Something like:
NSURL *baseURL = [NSURL URLWithString:@"www.abv.bg"];
After that you create relative path like:
NSString *path = @"/Path/to/somewhere";
And finally init URL string like this:
NSString *absoluteURL = [[NSURL URLWithString:path relativeToURL:baseURL] absoluteString];
And now you can execute request, and you know, that URL is correct:
[manager GET:absoluteURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@" Yep %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@" Nope %@", error);
}];
In your solution you try to execute request to address @"/api/data/interval?month" and not to real URL. HTH.
Upvotes: 1