Reputation: 31
I am doing this for defining headers in AFNetworking 4.0
NSMutableDictionary *headers = [[NSMutableDictionary alloc]init];
[headers setValue:[NSString stringWithFormat:@"Bearer %@",[defaults valueForKey:@"Token"]] forKey:@"Authorization"];
[manager POST:[NSURL URLWithString:path].absoluteString parameters:parameter headers:headers progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
}
And this is the method new POST method define in AFNetworking 4.0
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(nullable id)parameters
headers:(nullable NSDictionary <NSString *, NSString *> *)headers
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
{
}
While doing This is showing me error "[<__NSDictionary0 0x10c62a870> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Authorization."
Please tell me correct solution for this.
Thanks in Advance.
Upvotes: 1
Views: 3641
Reputation: 31
From the Above Given Answers I found a midway for the answers. Thanks for the Response @baiyidjp and @Silversky Technology
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
NSString *path=[[NSString alloc]initWithFormat:@"%@",URL];
NSMutableDictionary *parameter = [[NSMutableDictionary alloc]init];
[parameter setValue:@"123456" forKey:@"id"];
NSDictionary *headers = @{@"Authorization":[NSString stringWithFormat:@"Bearer %@",[defaults valueForKey:@"ApiToken"]]};
[manager POST:[NSURL URLWithString:path].absoluteString parameters:parameter headers:headers progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
NSLog(@"Response Object response is....==%@",responseObject);
}
failure:^(NSURLSessionTask *operation, NSError *error)
{
NSLog(@"Error Last 2 Done: %@", [error localizedDescription]);
}
Upvotes: 1
Reputation: 7193
Use this Block Method
+(void)postServiveCall:(NSDictionary *)perameters headers:(NSArray *)headers WithSuccess:(void (^)(id responseObject))success failure:(void (^)( NSError *error))failure
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
if(headers.count > 0)
{
for (NSDictionary *headerData in headers)
{
[manager.requestSerializer setValue:headerData.allValues.firstObject forHTTPHeaderField:headerData.allKeys.firstObject];
}
}
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
manager.responseSerializer = responseSerializer;
NSString *url = [NSString stringWithFormat:@"%@action.php",BASEURL];
[manager POST:url parameters:perameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
success (responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure (error) ;
}];
}
How to use this Give Below, Here you can Add multiple header field if you want
NSDictionary *postparam = @{@"action":@"versionCode",
@"version":version,@"device_type":@"iOS"
};
NSArray *heder = @[@{@"Authorization":[NSString stringWithFormat:@"Bearer %@",[defaults valueForKey:@"Token"]]}];
[ServiceCalls postServiveCall:postparam headers:heder WithSuccess:^(id responseObject)
{
} failure:^(NSError *error)
{
}];
Upvotes: 0
Reputation: 21
I am use AFN 4.0 now, but I add header also on manager like this:
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
[sessionManager.requestSerializer setValue:bearerToken forHTTPHeaderField:@"Authorization"];
and GET/POST/PUT/DELETE header I send @{} to backend
I hope I can help you
Upvotes: 2