Making a simple POST req from Swift to Apache/PHP.
Swift code:
let request = NSMutableURLRequest(URL: NSURL(string: "http://dzr.lenyapugachev.ru/createMember")!)
request.HTTPMethod = "POST"
let postString = "id=13&name=Jack"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
println("response = \(response)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
}
task.resume()
PHP:
<?php
echo $_SERVER['REQUEST_METHOD']."\n";
var_dump($_POST);
?>
Output:
GET
array(0) {}
So, it doesn't act as POST for server. I've also tried Alamofire and SwiftHTTP, same effect.
Please, good people, help.
It is indeed a HTTP POST request. Nothing wrong with your code. Using Charles (a HTPP debug proxy) I see that your request get redirected (301 HTTP)
You need to fix your server's code / .httpaccess configuration
Request:
POST /createMember HTTP/1.1
Host dzr.lenyapugachev.ru
Accept-Encoding gzip, deflate
Content-Type application/x-www-form-urlencoded
Content-Length 15
Accept-Language en-us
Accept */*
Connection keep-alive
User-Agent 29530174/1 CFNetwork/711.2.23 Darwin/13.4.0
Response:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://dzr.lenyapugachev.ru/createMember/">here</a>.</p>
</body></html>