Suyash Shetty Suyash Shetty - 28 days ago 6
iOS Question

How to send an object as a parameter for a POST request in iOS?

I need to bind parameters in an object and pass the object as a POST request to receive a successful piece of information from an API.

{
customer = {
"auth_token" = "";
"device_id" = 3e708bf1a49cdd06;
"email_address" = "abc@xyz.in";
name = abc;
number = 1234567890;
"resend_token" = true;
};
}


This is the object that I need to send along with the post request. But when I convert it into a string and post it, the entire object becomes the key and the value becomes
nil
. It gets posted as
{"{customer.....}=>nil}
.

The object should be posted as

{"customer:
{"auth_token":"","device_id":"3e708bf1a49cdd06","email_address":"abc@xyz.in",
"name":"abc","number":"1234567890","resend_token":"true"}}


This my current attempt:

NSArray *objects = [[NSArray alloc] initWithObjects:@"",@"3e708bf1a49cdd06",@"abc@xyz.in",@"abc",@"1234567890",@"true", nil];

NSArray *keys = [[NSArray alloc] initWithObjects:@"auth_token",@"device_id",@"email_address",@"name",@"number",@"resend_token", nil];

NSDictionary *tempJsonData = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

NSDictionary *finalJsonData = [[NSDictionary alloc] initWithObjectsAndKeys:tempJsonData,@"customer", nil];

NSData *temp = [NSJSONSerialization dataWithJSONObject:finalJsonData options:NSJSONWritingPrettyPrinted error:nil];
NSString *postString = [[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];

[request setHTTPMethod:@"POST"];
NSError *error = nil; NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


A lot of the code used here was used without a proper understanding and directly taken from other StackOverflow answers, so please excuse any bad programming practice.

How can I do this? Any help is appreciated. Thank you.

Answer Source

you can try below code.Instead of converting data to string set it as HTTPBody like

// Create the request.
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

// Specify that it will be a POST request
request.HTTPMethod = @"POST";

// This is how we set header fields
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];


// Convert your data and set your request's HTTPBody property

NSArray *objects = [[NSArray alloc] initWithObjects:@"",@"3e708bf1a49cdd06",@"abc@xyz.in",@"abc",@"1234567890",@"true", nil];

NSArray *keys = [[NSArray alloc] initWithObjects:@"auth_token",@"device_id",@"email_address",@"name",@"number",@"resend_token", nil];

NSDictionary *tempJsonData = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

NSDictionary *finalJsonData = [[NSDictionary alloc] initWithObjectsAndKeys:tempJsonData,@"customer", nil];

NSData *temp = [NSJSONSerialization dataWithJSONObject:finalJsonData options:NSJSONWritingPrettyPrinted error:nil];


request.HTTPBody = temp;


// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request setHTTPMethod:@"POST"];
 [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
NSError *error = nil; NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];