ios - AFNetworking 3 x-www-form-urlencoded post data -
i'm trying post data x-www-form-urlencoded body. posting via postman, ok
but cant via afnetworking 3. here code
nsdictionary *parameters = @{@"login" : email, @"password": password}; nserror *error; nsdata *jsondata = [nsjsonserialization datawithjsonobject:parameters options:0 error:&error]; nsstring *jsonstring = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; self.requestserializer = [afjsonrequestserializer serializer]; nsstring *urlstring = [nsstring stringwithformat:@"%@/%@", httpbaserequesturl, appendloginurl]; nslog(@"url %@\njsonstring %@", urlstring, jsonstring); [self post:urlstring parameters:nil constructingbodywithblock:^(id<afmultipartformdata> _nonnull formdata) { [formdata appendpartwithformdata:jsondata name:@"data"]; } progress:nil success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { onsuccess(responseobject); } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { nsstring *errordescription = [nserror servererrormessagefromdata:(nsdata *)error.userinfo[afnetworkingoperationfailingurlresponsedataerrorkey]]; nsinteger statuscode = [nshttpurlresponse errorcode:(nshttpurlresponse*)task.response]; networkrequesterror *requesterror = [[networkrequesterror alloc] initwithtype: (nshttpurlresponse*)task.response ? networkrequesterrortypeservererror : networkrequesterrortypenoconnection description: (nshttpurlresponse*)task.response ? errordescription : nil]; requesterror.statuscode = statuscode; nslog(@"error server: %@, status code = %ld, error type = %lu", requesterror.errordescription, (long)requesterror.statuscode, (unsigned long)requesterror.type); onfailure(requesterror); }];
please, me understand how correctly this. thanks!
after commenting found answer this. here's correctly functioning request now, note addition of
[manager.requestserializer setvalue:@"application/x-www-form-urlencoded; charset=utf-8" forhttpheaderfield:@"content-type"];
here's full code:
nsstring *url = [nsstring stringwithformat:@"%@%@",apibase,apiuserendpoint]; nsdictionary* parametersdictionary = [nsdictionary dictionarywithobjectsandkeys: username, @"username", password, @"password", nil ]; afhttpsessionmanager *manager = [[afhttpsessionmanager alloc]initwithsessionconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration]]; [manager.requestserializer setvalue:@"application/x-www-form-urlencoded; charset=utf-8" forhttpheaderfield:@"content-type"]; manager.requestserializer = [afhttprequestserializer serializer]; [manager post:url parameters:parametersdictionary progress:nil success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { nslog(@"%@",responseobject); } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { nslog(@"%@",error); }];
Comments
Post a Comment