Reputation: 157
That is my first time that I am using AFNetworking
in my app. I've installed the latest version (3.0) using pods . Now the problem is the url is working fine in browser and postman but when I get try in app it gives me unauthorised error. I don't know how to authorise. I am adding username and password in header. Can anybody help me?
Below is my code:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:username forHTTPHeaderField:@"username"];
[manager.requestSerializer setValue:password forHTTPHeaderField:@"password"];
[manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
failure(error);
}];
Following is full error log
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)"
UserInfo={NSUnderlyingError=0x7fa1c3f945b0 {Error
Domain=com.alamofire.error.serialization.response Code=-1016 "Request
> failed: unacceptable content-type: text/html"
> UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse:
0x7fa1c3d139f0> { URL:
http://192.168.0.111/guesswhat/getcategories.json } { status code:
401, headers {
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = "Keep-Alive";
"Content-Length" = 89;
"Content-Type" = "text/html";
Date = "Mon, 11 Jan 2016 12:30:55 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
"Keep-Alive" = "timeout=5, max=99";
Pragma = "no-cache";
Server = "Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.19";
"Www-Authenticate" = "Digest realm=\"Restricted area\",qop=\"auth\",nonce=\"5693a07f60c93\",opaque=\"cdce8a5c95a1427d74df7acbf41c9ce0\"";
Blockquote "X-Powered-By" = "PHP/5.5.19"; } },
Upvotes: 0
Views: 1582
Reputation: 934
The problem seems to be in your request formation. Based on the error log, please set the request content type, auth header and http method type. Services mostly use base64 string for user name and password auth field example code:
//base 64 auth value from username and password
- (void)setAuthorizationHeaderFieldWithUsername:(NSString *)username
password:(NSString *)password
{
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
[self setValue:[NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)] forHTTPHeaderField:@"Authorization"];
}
// authValue is your base 64 string from username and password
[manager.requestSerializer setValue:authValue forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
Upvotes: 0
Reputation: 549
It happens because of this :
NSLocalizedDescription=Request failed: unacceptable content-type: text/html
format Content-Type" = "text/html doesn't have your AFNetworking.
so simply go to Serilization->AFURLResponseSerialization.m, line 215, and change it:
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
it will work for you.
More recent versions may have the code on line 223
Upvotes: 0
Reputation: 14514
Try to set value for "Authorization" field on request serializer.
[manager.requestSerializer setValue:@"AuthToken" forHTTPHeaderField:@"Authorization"];
Upvotes: 0
Reputation: 8802
Try this code send your parameter in NSDictionary.(There is no base url in your code also passing parameter nil).
NSDictionary *parameters;
///// HTTP GET REQUEST TO DELETE ARTWORK
parameters = @{@"device_user_id": [[NSUserDefaults standardUserDefaults] objectForKey:@"UserID"],
@"device_auth_key": [[NSUserDefaults standardUserDefaults] objectForKey:@"AuthKey"],
@"query":@"",
@"user_id":[NSNumber numberWithInteger:[[[self.otherUserDictionary objectForKey:@"from_user"] objectForKey:@"id"] intValue]],
@"mobile_request":@"yes"};
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL_STRING]];//Here your base url string
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer =[AFJSONResponseSerializer serializerWithReadingOptions: NSJSONReadingMutableContainers];
// /search replace accordingly
[manager GET:@"/search" parameters:parameters success:^(NSURLSessionDataTask *task, NSDictionary* responseObject) {
//NSLog response object
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
Upvotes: 0