kudzai mhou kudzai mhou - 3 months ago 30
HTTP Question

Objective-C How to send data to Http Post request with nested dictionary in header

I have been learning objective-C for a few months now. In that time i have been able to do a basic post request however i saw today a header of a post request that contains a nested dictionary. I am not sure how to pass data to such an API.

My question is how can i go about posting data to this API. I would appreciate if you could point to relevant reading material and tutorials with examples such as this complex API requests or give answers.

The API header looks like this:

{ "header":
{ "id": "2312b24c-5b83-46f0-8670-5edc2b1d1672",
"parentId": "3gyy-w325e-bebd-ddsfsdf",
"countryCode": "IRELAND",
"timestamp": "2017-10-13T08:39:14.638Z" },
{ "User": "intro",
"contactPerson": "Newbee",
"contact": "00000000",
"notes": "hey guys i am new"



This is my code i need help with the multiple dictionaries.

__block NSMutableDictionary *resultsDictionary;
SMAMobileAPI *api = [SMAMobileAPI sharedSMAMobileAPI];
NSMutableDictionary *bodyParams = [[NSMutableDictionary alloc]init];

NSMutableDictionary *header = [[NSMutableDictionary alloc]init];
[header setValue:@"2312b23c-5b83-46f0-8670-5edc2b1d1672" forKey:@"id"];
[header setValue:@"2e3a015e-bebd-ddsfsdf" forKey:@"parentId"];
[header setValue:@“IRELAND" forKey:@"countryCode"];
[header setValue:@"2017-10-13T08:39:14.638Z" forKey:@"timeStamp"];

NSMutableDictionary *myinfo = [[NSMutableDictionary alloc]init];
[Case setValue:@“intro" forKey:@“User"];
[Case setValue:@“newbie" forKey:@"contactPerson"];
[Case setValue:@"+254 724474140" forKey:@"contactPersonNo"];
[Case setValue:@"iOS case" forKey:@"notes"];

[bodyParams setValue:header forKey:@"header"];
[bodyParams setValue:myinfo forKey:@“myinfo"];

if ([NSJSONSerialization isValidJSONObject:bodyParams]) {//validate it
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:bodyParams options:NSJSONWritingPrettyPrinted error: &error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
NSURL* url = [NSURL URLWithString:@“my URL"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];//use POST
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:jsonData];//set data
__block NSError *error1 = nil;

//use async way to connect network
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse* response,NSData* data,NSError* error)
if ([data length]>0 && error == nil) {
resultsDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
NSLog(@"resultsDictionary is %@",resultsDictionary);
[self hideLoading];

} else if ([data length]==0 && error ==nil) {
NSLog(@" download data is null");
[self hideLoading];
} else if( error!=nil) {
NSLog(@" error is %@",error);
[self hideLoading];

Answer Source

Seems like converting the NSDictionary to JSON is your actual problem. See documentation https://developer.apple.com/documentation/foundation/jsonserialization

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:yourDictionary
     options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download