How to set ticket with AFHTTPSessionOperation or AFHTTPSessionManager?
Asked Answered
I

1

1

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

Interferon answered 31/3, 2017 at 21:21 Comment(0)
S
1

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.

Symphonious answered 31/3, 2017 at 23:16 Comment(1)
you are right! The problem came from my ticket, it doesn't have the right data.Interferon

© 2022 - 2024 — McMap. All rights reserved.