xcode - How to send recursive json request with nsmutableurlrequest in ios? -
xcode - How to send recursive json request with nsmutableurlrequest in ios? -
in ios create this
"{ "email":string, "password":string }"
json request body, passing nsdata create string
email=myname@domain.com&password=mypassword
to sethttpbody method of nsmutableurlrequest.this works fine im ok this.
but if want create this
"{ "post": { "title":string, "room_id":int, "content":string, } }"
json request body? tried create string combinations solve recursion didnt work out really. checked methods of nsmutableurlrequest couldnt find related solve this.
edit:
this creates post should fine, need equivalent string email=myname@domain.com&password=mypassword recursive case. when send info should not work. when send string provided works.
nsstring *usertoken = [appdelegate token]; nsstring *posttopic = @"111testtopic"; nsstring *postbody = @"111testbody"; nsdictionary *dict = @{@"post":@{@"title":posttopic,@"room_id":@"246",@"content":postbody}}; nsdata *body = [nsjsonserialization datawithjsonobject:dict options:nsjsonwritingprettyprinted error:nil]; nsstring *postlength = [nsstring stringwithformat:@"%d", [body length]]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; //send post nsstring *urlstring = [nsstring stringwithformat:@"http://mydomain.com/posts.json?auth_token=%@", usertoken]; [request seturl:[nsurl urlwithstring:urlstring]]; [request sethttpmethod:@"post"]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request setvalue:@"application/x-www-form-urlencoded;charset=utf-8" forhttpheaderfield:@"content-type"]; [request sethttpbody:body]; nsurlresponse *response; nsdata *postreply = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:nil];
try this
nsdictionary *dict = @{@"post":@{@"title":string,@"room_id":int,@"content":string}}; nsdata *body = [nsjsonserialization datawithjsonobject:dict options:nsjsonwritingprettyprinted error:&error];
the thought utilize nested dictionaries describe json , serialize them jsondata pass request.
ios xcode json api nsmutableurlrequest
Comments
Post a Comment