I'm trying to send a POST in Objective-C to a PHP script, but the $_POST winds up empty. Here is the Obj-C code:
// Create the JSON object that describes the request
NSError *error;
NSDictionary *requestContents = @{
@"action": @"X",
@"distributor": @"Y"
};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
options:0
error:&error];
// Create a POST request with the receipt data.
NSURL *registryURL=[NSURL URLWithString:@"https://example.com/registry/"];
NSMutableURLRequest *registryRequest=[NSMutableURLRequest requestWithURL:registryURL];
[registryRequest setHTTPMethod:@"POST"];
[registryRequest setHTTPBody:requestData];
// Make a connection on a background queue.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:registryRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
/* ... Handle error ... */
} else {
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!jsonResponse) { /* ... Handle error ...*/ }
/* ... Send a response back to the device ... */
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
}
}];
Here is the .htaccess on the server. Of particular note is that I am forcing HTTP requests to HTTPS and therefore would lose the POST on the redirect, but the above code is requesting on HTTPS already.
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Redirect HTTP to HTTPS
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule ^([^/\.]+)/?$ /index.php?urlseg1=$1 [L,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?urlseg1=$1&urlseg2=$2 [L,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?urlseg1=$1&urlseg2=$2&urlseg3=$3 [L,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?urlseg1=$1&urlseg2=$2&urlseg3=$3&urlseg4=$4 [L,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?urlseg1=$1&urlseg2=$2&urlseg3=$3&urlseg4=$4&urlseg5=$5 [L,QSA]
#RewriteRule (.*) index.php [L]
</ifModule>
Now in the PHP script, $_POST is blank. And no matter what method I use of printing the request headers, POST is simply MIA.
I'm thinking that either the Obj-C code isn't doing the request properly, or the .htaccess is killing it. I'm not an expert in mod_rewrite so perhaps someone knows.
Appreciate any help!