Struggling to figure this out. Saw many examples, not sure what I'm doing wrong here. Need some fresh eyes. I have an array and a dictionary which I'm trying to send through HTTPBody, then return either one, just to see it it's working. Here's what I have:
NSURL *url = [NSURL URLWithString:@"http://..."];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *postData = [NSString stringWithFormat:@"dictofcorners=%@&arrayofids=%@", dict, _idKeys];
NSString *length = [NSString stringWithFormat:@"%d", postData.length];
[request setValue:length forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([httpResponse statusCode] == 200) {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);
}
}];
Server side I just want to send back either the array or dictionary. The response is 200, data is null. How do I handle the data server-side? Or is there something different I should do to serialize the array and dictionary before I send the request?
<?php
$dictofcorners = json_decode($_REQUEST['dictofcorners']);
echo json_encode($dictofcorners);
//$arrayofids = json_decode($_REQUEST['arrayofids']);
//echo json_encode($arrayofids);
?>
ANSWER BELOW!