AFNetworking 2.0:传递标题信息

时间:2014-04-17 13:44:34

标签: ios objective-c afnetworking afnetworking-2

我是AFNetworking的新手,我知道如何传递网址参数。但是我如何将标题传递到同一个调用中。

我也是AFHTTPSessionManager的子类

请参阅下面的代码:

- (void)getExpenses:(NSString *)page
                success:(void (^) (NSArray *myExpenses))success
                failure:(RequestFailureBlock)failure
{
    NSString *resourceURL = [NSString stringWithFormat:@"%@/expenses/", APIBaseURLString];
    NSDictionary *parameters = @{@"page":page, @"Authorization": APIAuthorization};

    [self getExpenses:resourceURL parameters:parameters success:success failure:failure];

}

2 个答案:

答案 0 :(得分:6)

由于setAuthorizationHeaderFieldWithToken is deprecated而发送的{p> servers having different requirements about how the access token(令牌,承载等)

michaels回答是正确的,使用

[self.requestSerializer setValue:@"Some-Value" forHTTPHeaderField:@"Header-Field"];

[self.requestSerializer setAuthorizationHeaderFieldWithUsername:@"" password:@""];

用于基本身份验证

答案 1 :(得分:0)

您在AFHTTPSessionManager的requestSerializer属性上设置标题值:

[self.requestSerializer setValue:@"Some-Value" forHTTPHeaderField:@"Header-Field"];

编辑:

看起来你正试图设置授权;也有一种方法:

[self.requestSerializer setAuthorizationHeaderFieldWithUsername:@"" password:@""];
// OR
[self.requestSerializer setAuthorizationHeaderFieldWithToken:@""];

如果您需要设置Content-Type标题,请参阅this SO answer了解如何操作