AFNetworking 4.0 如何在 POST 请求中添加 headers?
How to add headers in AFNetworking 4.0 in POST Request?
我这样做是为了在 AFNetworking 4.0
中定义 headers
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)
{
}
这是AFNetworking 4.0
中定义的新POST方法
- (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
{
}
在执行此操作时显示错误“[<__NSDictionary0 0x10c62a870> setValue:forUndefinedKey:]:此 class 不是密钥授权的密钥值 coding-compliant。 “
请告诉我正确的解决方案。
提前致谢。
我现在使用 AFN 4.0,但我也在管理器上添加 header,如下所示:
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
[sessionManager.requestSerializer setValue:bearerToken forHTTPHeaderField:@"Authorization"];
and GET/POST/PUT/DELETE header I send @{} to backend
希望能帮到你
使用此块方法
+(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) ;
}];
}
如何使用下面给出,如果需要,您可以在这里添加多个header字段
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)
{
}];
从上面给出的答案中,我找到了答案的中途。感谢@baiyidjp 和@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]);
}
我这样做是为了在 AFNetworking 4.0
中定义 headers 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)
{
}
这是AFNetworking 4.0
中定义的新POST方法 - (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
{
}
在执行此操作时显示错误“[<__NSDictionary0 0x10c62a870> setValue:forUndefinedKey:]:此 class 不是密钥授权的密钥值 coding-compliant。 “
请告诉我正确的解决方案。
提前致谢。
我现在使用 AFN 4.0,但我也在管理器上添加 header,如下所示:
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
[sessionManager.requestSerializer setValue:bearerToken forHTTPHeaderField:@"Authorization"];
and GET/POST/PUT/DELETE header I send @{} to backend
希望能帮到你
使用此块方法
+(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) ;
}];
}
如何使用下面给出,如果需要,您可以在这里添加多个header字段
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)
{
}];
从上面给出的答案中,我找到了答案的中途。感谢@baiyidjp 和@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]);
}