ios - Call REST API having Content-Type: application/x-www-form-urlencoded and Authorization using NSURLSession -


how call rest api having content-type: application/x-www-form-urlencoded , authorization using nsurlsession

request url: https://<host>:<port>/signup request method: post content-type: application/x-www-form-urlencoded accept: application/json  authorization: mps abcdefgh 

input parameter

msg={     "data": {         "id": "10",         "req": "hdfc",         "typeid": "180",         "sd": "mno"     },     "cde": "code",     "key": "key",     "bccode": null,     "mccode": null,     "pincode": null } 

what tried far

-(void)mycallapi {  nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration];     nsurlsession *session = [nsurlsession sessionwithconfiguration:configuration delegate:self delegatequeue:nil];       nsstring *urlstring=[nsstring stringwithformat:@"https://<host>:<port>/signup"];     nsurl *url = [nsurl urlwithstring:urlstring];     nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url                                                            cachepolicy:nsurlrequestuseprotocolcachepolicy                                                        timeoutinterval:60.0];      [request addvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"];     [request addvalue:@"application/json" forhttpheaderfield:@"accept"];     nsstring *authvalue = [nsstring stringwithformat:@"mps abcdefgh"];     [request setvalue:authvalue forhttpheaderfield:@"authorization"];      [request sethttpmethod:@"post"];      nsdictionary *mapdata = [[nsdictionary alloc] initwithobjectsandkeys: @"test ios", @"name",      @"ios type", @"typemap",      nil];      nsdata *postdata = [nsjsonserialization datawithjsonobject:mapdata options:0 error:&error];      [request sethttpbody:mapdata];         nsurlsessiondatatask *postdatatask = [session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) {          nsstring* responsestring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];          if([responsestring rangeofstring:@"nil"].location != nsnotfound)         {             nsstring * newresponse =  [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding];              responsestring = newresponse;         }          nslog(@"%@",responsestring);         nslog(@"response %@",response);         nslog(@"error %@",error);       }];      [postdatatask resume];  } 

you need add header keys , values nsurlsessionconfiguration:

configuration.httpadditionalheaders["content-type"] = @"application/x-www-form-urlencoded"; 

do same other http headers (authorization in case).

also note values submitting ("test ios" , "ios type") not url encoded.

use stringbyaddingpercentencodingwithallowedcharacters encode them:

[@"test ios" stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset urlhostallowedcharacterset]]; 

Comments