AFNetworking 2.0 add headers to GET request

后端 未结 7 1117
天命终不由人
天命终不由人 2020-12-07 12:20

I\'ve just started using AFNetworking 2.0 and I was wondering how I put in headers into a HTTP Get request. The documentation sets up a GET like this:

AFHTTP         


        
7条回答
  •  不知归路
    2020-12-07 12:43

    Here's what i believe to be a best option. In a singleton somewhere, configure an AFHTTPSessionManager using an NSURLSessionConfiguration, and then use that AFHTTPSessionManager every time you want to make a request.

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    config.HTTPAdditionalHeaders = @{@"Accepts": @"application/json"};
    
    mySingletonSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kMyBaseUrl] sessionConfiguration:config];
    

提交回复
热议问题