i'm trying send post in objective-c php script, $_post winds empty. here obj-c code:
// create json object describes request nserror *error; nsdictionary *requestcontents = @{ @"action": @"x", @"distributor": @"y" }; nsdata *requestdata = [nsjsonserialization datawithjsonobject:requestcontents options:0 error:&error]; // create post request receipt data. nsurl *registryurl=[nsurl urlwithstring:@"https://example.com/registry/"]; nsmutableurlrequest *registryrequest=[nsmutableurlrequest requestwithurl:registryurl]; [registryrequest sethttpmethod:@"post"]; [registryrequest sethttpbody:requestdata]; // make connection on 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 response device ... */ nslog(@"%@", [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding]); } }];
here .htaccess on server. of particular note forcing http requests https , therefore lose post on redirect, above code requesting on https already.
<ifmodule mod_rewrite.c> rewriteengine on rewritebase / #redirect http 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 php script, $_post blank. , no matter method use of printing request headers, post mia.
i'm thinking either obj-c code isn't doing request properly, or .htaccess killing it. i'm not expert in mod_rewrite perhaps knows.
appreciate help!
you suspect .htaccess
, there problem here. code building post
request, body of request json. when that, $_post
not populated you. if want use $_post
, consider creating application/x-www-form-urlrequest
. in case, set httpbody
like:
key1=value1&key2=value2
for example:
action=x&distributor=y
you may want set content-type
of http header application/x-www-form-urlrequest
. (this isn't technically required, it's practice specify content-type
header correspond nature of body of request.)
furthermore, if x
, y
values might include characters other alphanumeric characters (e.g. if contains space, punctuation, or non-ascii characters), you'll want percent-escape values. see https://stackoverflow.com/a/25702239/1271826 example of how might in objective-c.
alternatively, if want make json request (which fine), can't use $_post
on server end, rather manually receive body of request , call json_decode
. example, see https://stackoverflow.com/a/27884399/1271826.
if don't want lost in weeds of proper preparation of these sorts of requests, consider using library afnetworking, takes care of these details you.
Comments
Post a Comment