1

I am trying to make an HTTP request from my iOS app to a PHP script in my server, sending POST data. The Objective-C code is the following:

            NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mywebsite.com/script.php"]
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:60.0];
            [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
            [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setHTTPMethod:@"POST"];

            NSMutableDictionary *info = [NSMutableDictionary dictionary];
            [info setObject:[NSNumber numberWithInt:5] forKey:@"FIRST"];
            [info setObject:[NSNumber numberWithInt:3] forKey:@"SECOND"];

            NSData *data = [NSJSONSerialization dataWithJSONObject:info options:0 error:&error];
            [request setHTTPBody:data];
            NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                NSLog(@"I GOT THE ANSWER: %@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
            }];

            [postDataTask resume];

And this is the PHP script:

<?php

$first  = intval($_POST["FIRST"]);
$second = intval($_POST["SECOND"]);

$result = $first + $second;

echo("= " . $result);

?>

The iOS app outputs

I GOT THE ANSWER: = 0

Which is incorrect. I'm not sure what the problem is.

I based my code on this solution: https://stackoverflow.com/a/19101084/555690. In that same question, the asker also posted another solution without using NSDictionary, which does work for me, but I don't see why is this particular solution not working.

Community
  • 1
  • 1
Saturn
  • 17,888
  • 49
  • 145
  • 271

1 Answers1

0

So you have to encode the string with NSUTF8StringEncoding.

In my experience, the easiest/best way to do this is to use a "post string" instead, so you can encode it.

Also, you want to add NSError *error = nil; or something like that in there. In addition, you probably want to add it to the main delegateQueue so you can handle it with its delegate.

Here's an example (and I tested it):

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

NSURL *url = [NSURL URLWithString:@"http://localhost:8888/Untitled.php"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *params =@"FIRST=1&SECOND=2";
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"Data = %@", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
}];

[dataTask resume];

I would also recommend using session_start(); at the top of the file.

Hope this helps!

iSkore
  • 7,394
  • 3
  • 34
  • 59