NSURLSession/NSURLConnection POST does not deliver data

I am updating an old app, and my old code which uploads POST messages to a PHP script is not working. It seems like the POST array is simply empty. I've looked online and my code looks just like all the examples I see.

just tried the answer given here, a simple example, and I get nothing in the POST array:

https://stackoverflow.com/questions/28654603/php-post-script-works-with-other-sites-but-not-with-ios-nsurlconnection

Here is the code from that example that does not work for me:


@implementation Test : NSObject
-(void)userLogin {
    NSString *user = @"test";
    NSString *pass = @"pass";

    NSString *post = [NSString stringWithFormat: @"user=%@&pass=%@", user, pass];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:@"www.example.com/login.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    sleep(50);

    NSLog(@"%@", conn);
}

@end



--------------- PHP file -----------------------

<?php
  file_put_contents('/tmp/test', print_r($_POST, true));
?>


On the other hand, if I go to a site like this and send a message with a simple 'user=me' and 'pass=whatever', then my php script does show that it received the variables.

https://reqbin.com/post-online

Furthermore, if I go to a site that will receive POSTs, like this one, then the example code above works fine.

http://ptsv2.com

Anyone know what could be the issue?

Answered by bsabiston in 705468022

Ugh I cannot even explain the solution because of this forum's HTTP rules.  Hence the spaces in these URLs... And even once I correct them, the buggy forum software doesn't recognize I've done it. I have to start over.

So for anyone else getting this, I managed to fumble through it.  Using the 'ptv2' site I was able to send my POST without problem.  That made me think the problem was on the server side.  Then I read online about how people were seeing POSTs converted to GETs when the data was empty - or when there was a redirect.  What solved it for some people was changing 'www .mysite.com' to 'mysite.com'.  I tried this, but it did not work for me.  However, it may have been one step in the solution, something I needed to do.

I confirmed on the PHP side that it did indeed think it was getting a GET and not a POST.  But why though...

I noticed that when I went to 'http : //mysite.com', in the browser it was being converted to 'https : //mysite.com'. That was the solution. I needed to change 'http' to 'https' (and probably, " www .blah.com" to "blah.com").

The first step with problems like this is to look at the data on the ‘wire’ to determine whether the problem is on the client side or the server side. The best tool for that is the shiny new HTTP Traffic instrument. See Analyzing HTTP Traffic with Instruments for more on that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Ugh I cannot even explain the solution because of this forum's HTTP rules.  Hence the spaces in these URLs... And even once I correct them, the buggy forum software doesn't recognize I've done it. I have to start over.

So for anyone else getting this, I managed to fumble through it.  Using the 'ptv2' site I was able to send my POST without problem.  That made me think the problem was on the server side.  Then I read online about how people were seeing POSTs converted to GETs when the data was empty - or when there was a redirect.  What solved it for some people was changing 'www .mysite.com' to 'mysite.com'.  I tried this, but it did not work for me.  However, it may have been one step in the solution, something I needed to do.

I confirmed on the PHP side that it did indeed think it was getting a GET and not a POST.  But why though...

I noticed that when I went to 'http : //mysite.com', in the browser it was being converted to 'https : //mysite.com'. That was the solution. I needed to change 'http' to 'https' (and probably, " www .blah.com" to "blah.com").

NSURLSession/NSURLConnection POST does not deliver data
 
 
Q