objective c - How to pass an HTTP as a header in with NSURLSession -
i'm trying submit request web service , want pass json argument parameter in http header. have following code performs request without json argument. how add json argument in http body pass json parameter?
heres code works without json argument:
-(void) getreposbydate:(void (^)(nsmutablearray *))handler { //get credentials nsdictionary *credentials = [keychainuserpass load:@"app name"]; nsstring *username = [credentials allkeys][0]; nsstring *password = credentials[username]; //create request nsstring *requeststring = @"some web service url"; nsurl *url = [nsurl urlwithstring:requeststring]; nsurlrequest *req = [nsurlrequest requestwithurl:url]; nsdata *userpassworddata = [[nsstring stringwithformat:@"%@:%@", username, password] datausingencoding:nsutf8stringencoding]; nsstring *base64encodedcredential = [userpassworddata base64encodedstringwithoptions:0]; nsstring *authstring = [nsstring stringwithformat:@"basic %@", base64encodedcredential]; nsurlsessionconfiguration *sessionconfig=[nsurlsessionconfiguration defaultsessionconfiguration]; sessionconfig.httpadditionalheaders=@{@"authorization":authstring}; self.session=[nsurlsession sessionwithconfiguration:sessionconfig]; nsurlsessiondatatask *datatask = [self.session datataskwithrequest:req completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nsmutabledictionary *jsonobject = [nsjsonserialization jsonobjectwithdata:data options:0 error:nil]; nslog(@"%@", jsonobject); handler([jsonobject valueforkeypath:@"name"]); }]; [datatask resume]; }
Comments
Post a Comment