shine
shine

Reputation: 83

is it will be a good practice to call API with json object as input parameter rather than NSDictionary.

is it will be a good practice to call API with json object as input parameter rather than NSDictionary.

Usually we used to send NSDictionary as input parameters. Is there any issue or performance improvement in mobile device or in server side

Upvotes: 0

Views: 86

Answers (1)

Rob
Rob

Reputation: 437622

Yes, it is a fine practice to take your Objective-C NSDictionary, use NSJSONSerialization to convert that to JSON (or use a library like AFNetworking that can do that for you) and send the JSON in the network request you send when calling your web service. You would generally not send the NSDictionary itself (e.g. a plist or keyed archiver). JSON is the lingua franca of web services. (XML is another very common format, though JSON is easier on the iOS side, IMHO.)

If you're building the network request yourself (e.g. building a NSMutableURLRequest to be sent using NSURLSession), remember to:

  • set HTTPMethod to @"POST";
  • set the HTTPBody to be the JSON;
  • set the Content-Type HTTP header to application/json; and
  • set the Accept HHTP header to indicate what format you're expecting the response to be (also likely application/json).

Upvotes: 2

Related Questions