ios - Bad formatting: Add JSON data to existing URL -
i need send request this:
"http://api.site.com/login?q={"meta":{"api_key":"cb2f734a14ee3527b3"},"request":{"id":"username@host.name","password":"passw0rd"}}`
...the response should {"id":399205,"token":"d43f8b2fe37aa19ac7057701"}
so, have tried following code:
self.responsedata = [nsmutabledata data]; nsdictionary *apikeydict = [nsdictionary dictionarywithobject:@"cb2f734a14ee3527b3" forkey:@"api_key"]; nsdictionary *idpassworddict = [nsdictionary dictionarywithobjectsandkeys:@"tc-d@gmail.com",@"id", @"abc",@"password", nil]; nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys:apikeydict, @"meta", idpassworddict, @"request", nil]; nsurl *url = [nsurl urlwithstring:@"http://api.site.com/login?="]; nserror *error = nil; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0]; nsdata *requestdata = [nsjsonserialization datawithjsonobject:dict options:nsjsonwritingprettyprinted error:&error]; [request sethttpmethod:@"post"]; [request setvalue:@"application/json" forhttpheaderfield:@"accept"]; [request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request setvalue:[nsstring stringwithformat:@"%d", [requestdata length]] forhttpheaderfield:@"content-length"]; [request sethttpbody: requestdata]; nsurlconnection *connection = [[nsurlconnection alloc]initwithrequest:request delegate:self]; if (connection) { self.recieveddata = [nsmutabledata data]; }
later on, in following:
- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [self.recieveddata appenddata:data]; nslog(@"%@",[[nsstring alloc]initwithdata:data encoding:nsutf8stringencoding]); }
the response data comes "<h1>chttpexception</h1><p>wrong sign</p>
" understand, way of adding json data current url not appropriate in case. have advice on how can fix this?
resolve problem. convert posted json appended string base url-string. that's how did it:
nsdata *requestdata = [nsjsonserialization datawithjsonobject:dict options:nsjsonwritingprettyprinted error:&error]; nsmutablestring *dictstring = [[nsmutablestring alloc]initwithdata:requestdata encoding:nsutf8stringencoding]; [dictstring insertstring:@"http://api.site.com/login?q=" atindex:0]; //don't know why dictstring contains lot of @"\n" , spaces present nice-formated. [dictstring replaceoccurrencesofstring:@"\n" withstring:@"" options:nil range:nsmakerange(0, dictstring.length)]; [dictstring replaceoccurrencesofstring:@" " withstring:@"" options:nil range:nsmakerange(0, dictstring.length)]; nsurl *url = [nsurl urlwithstring:[dictstring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];
if helps appreciate tick near issue=)
you can right using afnetworking library - it's easy implement , work with. whole code need in case quite simple , working me.
nsdictionary *apikeydict = [nsdictionary dictionarywithobject:@"cb2f734a14ee3527b3" forkey:@"api_key"]; nsdictionary *idpassworddict = [nsdictionary dictionarywithobjectsandkeys:@"tc-d@gmail.com",@"id", @"abc",@"password", nil]; nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys:apikeydict, @"meta", idpassworddict, @"request", nil]; nsdata *jsondata = [nsjsonserialization datawithjsonobject:dict options:0 error:nil]; nsstring *jsonstring = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; nsstring *urlstring = @"http://api.site.com/login"; nsdictionary *parameters = [nsdictionary dictionarywithobjectsandkeys:jsonstring, @"q", nil]; nsurl *url = [nsurl urlwithstring:urlstring]; afhttpclient *httpclient = [[afhttpclient alloc] initwithbaseurl:url]; nsmutableurlrequest *request = [httpclient requestwithmethod:@"post" path:nil parameters:parameters]; afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { //ok nslog(@"%@", json); } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json) { //response failed }]; [operation start];
Comments
Post a Comment