ios - add custom header to either NSURLRequest or AFHTTPRequestOperation -
i have project need add together custom header auth_token to. it's bit unclear me how add together afhttprequestoperation. have:
nsurlsessionconfiguration *sessionconfiguration = [nsurlsessionconfiguration defaultsessionconfiguration]; // configure session configuration [sessionconfiguration setallowscellularaccess:yes]; [sessionconfiguration sethttpadditionalheaders:@{ @"auth_token" : [self.credentialstore authtoken]}]; // create session nsurlsession *session = [nsurlsession sessionwithconfiguration:sessionconfiguration]; nsurl *url = [nsurl urlwithstring:[nsstring stringwithformat:@"%@/arc/v1/api/mi/images",myhost()]]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; //[manager.requestserializer setvalue:[self.credentialstore authtoken] forhttpheaderfield:@"auth_token"]; afhttprequestoperation *op = [[afhttprequestoperation alloc] initwithrequest:request]; op.responseserializer = [afjsonresponseserializer serializer]; [op setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { ... [[nsoperationqueue mainqueue] addoperation:op];
how add together auth_token custom header?
in other parts use:
afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; [manager.requestserializer setvalue:[self.credentialstore authtoken] forhttpheaderfield:@"auth_token"];
if that's preferred way, can migrate code on style.
thx help
setting on request serializer recommended method when using afnetworking. however, looks you're using nsurlsession directly. i'd recommend deciding on 1 networking approach before deciding set custom header.
ios afnetworking-2 nsurlrequest
No comments:
Post a Comment