Search code examples
afnetworking-3

How to set ticket with AFHTTPSessionOperation or AFHTTPSessionManager?


Does anyone know how to set ticket inside AFHTTPSessionOperation? This is the previous call using AFNetworking framework 1.0

NSURLRequest* request = [self.myClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"%@/%@", controller, action] parameters:parameters];
AFHTTPRequestOperation* operation = [self.myClient HTTPRequestOperationWithRequest:request success:success failure:failure];
[self.mirrorClient enqueueHTTPRequestOperation:operation];

The ticket is stored inside the self.myClient. self.myClient.ticket

But I'm not sure how to implement that in the following call using AFHTTPSessionOperation with AFNetworking framework 3.1.

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer = manager.requestSerializer;
[requestSerializer  setValue:[NSString stringWithFormat:@"%@", self.myClient.ticket] forHTTPHeaderField:@"Authorization"];

NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:@"POST" 
URLString:urlString parameters:parameters 
uploadProgress:nil downloadProgress: nil 
success:success failure:failure];

Thank you


Solution

  • This code looks basically correct. You could simplify the requestSerializer configuration a tad, and I might not instantiate a new session for every request, but the following worked fine for me:

    - (void)performRequest:(NSString *)urlString
                parameters:(id)parameters
                   success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success
                   failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager.requestSerializer setValue:self.myClient.ticket forHTTPHeaderField:@"Authorization"];
    
        NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager
                                                                   HTTPMethod:@"POST"
                                                                    URLString:urlString
                                                                   parameters:parameters
                                                               uploadProgress:nil
                                                             downloadProgress:nil
                                                                      success:success
                                                                      failure:failure];
        [self.queue addOperation:operation];
    }
    

    I watched it in Charles, and the ticket, 12345678 appeared in my request header, as expected:

    enter image description here

    I suspect your problem rests elsewhere. This code does set the Authorization header to ticket. Make sure this is the right place to set the ticket. Also, make sure the ticket is what you think it is.