0

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!

denikov
  • 877
  • 2
  • 17
  • 35

1 Answers1

0

How I got it to work...apparently:

NSMutableDictionary *paramDic = [NSMutableDictionary new];
[paramDic setValue:dic forKeyPath:@"dic"];
[paramDic setValue:_idKeys forKeyPath:@"array"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:paramDic options:NSJSONWritingPrettyPrinted error:nil];

NSURL *url = [NSURL URLWithString:@"http://....php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSString *length = [NSString stringWithFormat:@"%d", jsonData.length];
[request setValue:length forHTTPHeaderField:@"Content-Length"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setHTTPBody:jsonData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
    if ([httpResponse statusCode] == 200) {
//...

php part:

$input = file_get_contents("php://input");
$obj = json_decode($input, true);
$dic = $obj['dic'];
$array = $obj['array'];

I don't understand the php part but it works. Basically just needed to create one object to pass in the body...so I created a dictionary and added everything that I needed to pass. If anyone wants to explain the php part, the php://input part, I'd be happy to hear it!

denikov
  • 877
  • 2
  • 17
  • 35