Reputation: 11
I'm trying to do this with Objective-C but i think is the same body data but a don't know how it has to be.
Upvotes: 0
Views: 1882
Reputation: 11
I found the way to do this:
POST request
head: </*Your route*/>
body:
p=aString&p=bString&p=cString&p=dString
And then in the server you get:
body : {
p : [{
"aString",
"bString",
"cString",
"dString"
}]
}
Thanks for the support :)
Upvotes: 1
Reputation: 1590
Here are two methods I used in an old project. You'll need to set the NSURLConnectionDelegate and implement the required methods.
// Check if the URLString is a valid data object
- (void) formatURLString: (NSString *) target forData:(NSArray *) data
{
if ([NSJSONSerialization isValidJSONObject:data])
{
NSError *error;
NSData *JSONdata = [NSJSONSerialization dataWithJSONObject:data options:kNilOptions error:&error];
// *** POST ***//
[self post:target withJSON:JSONdata];
} else
{
NSLog(@"Invalid JSON object. JSON object: %@", data);
}
}
- (void) post: (NSString *) target withJSON: (NSData *) jsonData
{
// Inform user network activity is taking place
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSMutableURLRequest *urlRequest =
[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:target]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
[urlRequest setHTTPBody:jsonData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self];
if (!connection) { NSLog(@"Connection Failed"); }
}
Upvotes: 0
Reputation: 1491
One way (not sure the best way) is to encode the array in the POST data by sending the # of elements as a POST data field. For example
NSArray *array = @[@"hi", @"there"];
NSMutableString *postData = [NSMutableString stringWithFormat:@"array_length=%d", array.count];
for (int i=0; i<array.count; ++i)
[postData appendFormat:@"&array_%d=%@", i, array[i]];
// ...
The decoder, then, takes array_length
and iterates over i = [0 .. array_length-1]
to repopulate the array with array_0, array_1, ...
.
Upvotes: 0