Reputation:
i try many solutions from stackoverflow to fix this trouble, but no luck. My trouble is setHTTPMethod not works thats always use GET method. My code is:
NSHTTPURLResponse *response;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverAPIUrl]];
NSMutableData *requestBody = [NSMutableData data];
NSString *boundary = @"ljlkjlkkljlk98jjmnj";
[request setHTTPMethod:@"POST"];
[request setCachePolicy: NSURLRequestReloadIgnoringCacheData];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField: @"Content-Type"];
[requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",@"file",@"file.gz"] dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[self gzipData:returnData]];
NSMutableArray *keys=[[NSMutableArray alloc] init];
NSMutableArray *vals=[[NSMutableArray alloc] init];
//post vars
[keys addObject:@"postvariable"];
[vals addObject:@"kjasdhfklsadjfhsajdkfhasdfjhsadfkjsadfhskdfjhjhf"];
//add post vars to body
for(i=0;i<[keys count];i++)
{
[requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\nContent-Disposition: form-data; name=\"%@\"; \r\n\r\n%@",boundary,
[keys objectAtIndex:i],
[vals objectAtIndex:i]
] dataUsingEncoding:NSUTF8StringEncoding]];
}
//add post vars to body
[requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:requestBody];
NSData *answer = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: nil ];
NSLog(@"server answer:%@",[[NSString alloc] initWithData:answer encoding:NSUTF8StringEncoding]);
PHP on server
if(isset($_REQUEST["getjob"]))
{
}
else
{
foreach ($_SERVER as $name => $value) {
echo "$name: $value\n";
}
var_dump($_FILES);
var_dump($_POST);
echo file_exists($_FILES["file"]["tmp_name"]);
echo "ok";
}
What i have:
REMOTE_PORT: 57688
GATEWAY_INTERFACE: CGI/1.1
SERVER_PROTOCOL: HTTP/1.1
REQUEST_METHOD: GET
QUERY_STRING:
REQUEST_URI: /dcapi/
Thats server answer just returns $_SERVER variables. I always getting GET method. But i always use POST.
I tried many solutions from stackoverflow with no luck:
http://kemal.co/index.php/2012/02/fetching-data-with-getpost-methods-by-using-nsurlconnection/ - i just download sources. With no result.
HTTP Post Request in Objective-C Not Working not works.
Upload file not working vie HTTP Post not works.
POST from iPhone to PhP not working not works.
Upvotes: 1
Views: 765
Reputation:
Ok. I solve it. Thats because i set end of url as folder/ but executing file is in folder/index.php. And i getting 301 redirect for my post request and only get result from second request as GET.
Upvotes: 1