Post data in Objective C using Json

I think you would be better off using the NSJSONSerialization class like this:

NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
                     email, @"Email",
                     fname, @"FirstName",
                     nil];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
[request setHTTPBody:postData];

Using a dictionary and then converting it to JSON it’s easier than creating it like a string.

Good luck!

[SWIFT 3.0] (update)

let tmp = ["email": email,
           "FirstName": fname]
let postData = try? JSONSerialization.data(withJSONObject: tmp, options: .prettyPrinted)
request.httpBody = postData

Leave a Comment