AFNetworking似乎没有使用AFJSONRequestOperation发送授权标头

use*_*464 5 authorization header afnetworking

我正在尝试使用AFNetworking和AFJSONRequestOperation发送Authorization标头.如果我在setAuthorizationHeaderWithToken之后NSLog的httpClient,它将显示它在标题中,但是当我检查它发送到的服务器时,它似乎没有收到Authorization标头(它接收其他部分).

AFJSONRequestOperation是否对未添加授权部分的标题执行某些操作?

NSURL *url = [NSURL URLWithString:kBaseURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

 NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/" parameters:params];   

[httpClient setAuthorizationHeaderWithToken:@"test"];

AFJSONRequestOperation *operation = nil;

operation = [AFJSONRequestOperation
             JSONRequestOperationWithRequest:request
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

             }
             failure:^(NSURLRequest *request , NSHTTPURLResponse *response, NSError *error , id JSON ){

             }];

[operation start];
Run Code Online (Sandbox Code Playgroud)

NSURL *url = [NSURL URLWithString:kBaseURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

 NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/" parameters:params];   

[httpClient setAuthorizationHeaderWithToken:@"test"];

AFJSONRequestOperation *operation = nil;

operation = [AFJSONRequestOperation
             JSONRequestOperationWithRequest:request
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

             }
             failure:^(NSURLRequest *request , NSHTTPURLResponse *response, NSError *error , id JSON ){

             }];

[operation start];
Run Code Online (Sandbox Code Playgroud)

Kei*_*ley 2

NSMutableURLRequest您是否尝试过在使用 httpClient 创建(在该代码块中似乎不需要可变)之前添加标头?看起来您直到创建请求之后才添加令牌标头,在这种情况下 AFJSONRequestOperation 甚至看不到标头,因为实际上在该函数的源代码AFHTTPClient中只是添加 HTTP 标头的便捷方法名称“授权”