-1

I am making an API call in which the Parameters are as:

{
      name : XYZ,
      marks : { maths : 34 }
}

I am using JSON model for making API calls and handling the response.

Here is how I made params Dict:

NSMutableDictionary *paramsDict = [[NSMutableDictionary alloc] init];

NSMutableDictionary *marksDict = [[NSMutableDictionary alloc] init];
[marksDict setObject:"34" forKey:"maths"];

[paramsDict setObject:marksDict forKey:@"marks"];

[paramsDict setObject:@"XYZ" forKey:@"name"];

Here Goes my API CALL

APIManager *apimanager = [[APIManager alloc] init];
apimanager.delegate = self;

[apimanager getServerDataWithParam:paramsDict apiMethodType:kAPIMethodTypePOST andMapperClass:[StudentMarks class]];

My App Crashes giving me this Crash Log

Assertion failure in +[JSONHTTPClient urlEncode:], /Users/Documents/JSONHTTPClient.m:122
2016-08-12 12:46:16.653 ClassMgmt[30227:1338857] *** Terminating app due to uncaught exception 
'NSInternalInconsistencyException', reason: 'request parameters can be only of NSString or NSNumber classes.
'{   maths = 34; }' is of class __NSDictionaryM.'

I don't want to convert my NSDictionary to NSString using:

[NSString stringWithFormat:@"@", aDictionary];

Is their any other way I can send NSDictionary as a value in POST Request as a parameter?

Rahul
  • 5,594
  • 7
  • 38
  • 92

2 Answers2

0

Long-story-short; HTTP POST parameters can only be name/value pairs without hierarchy.

If you want to post JSON, on the other hand, that is part of the HTTP POST body, not the parameters.

Droppy
  • 9,691
  • 1
  • 20
  • 27
0
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil]
NSString *paramsjsonStr = [[NSString alloc] initWithData: jsonData encoding:NSUTF8StringEncoding]
ocarol
  • 283
  • 1
  • 5