Reputation:
I am trying post request in swift. I did successfully in objective-C but using swift I could not send request. $emailOK
in PHP could not get information. where is the problem? thanks for helper. here is swift code
func httpPost1(url:String, postData: String, completion: String -> Void) {
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
request.HTTPMethod = "POST"
let postString = postData
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
}
here is excution code
httpPost1("xxxxx", postData: "emailOK=Hello") { result in
print(result)//result is your string-response from server
}
Here is my php code
<?php
$link = mysqli_connect($dbhost, $username, $dbpass, $database);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
$emailOK = isset($_GET["emailOK"]) ? $_GET["emailOK"] : '';
echo $emailOK;
$query = "INSERT INTO UserInfo VALUES ('', '$emailOK')";
mysqli_query($link, $query) or die (mysqli_error("error"));
mysqli_close($link);
?>
Upvotes: 0
Views: 113
Reputation:
I found the problem, thanks for @OOPer comment. the problem is in PHP $emailOK = isset($_GET["emailOK"]) ? $_GET["emailOK"] : '';
It should be $emailOK = $_POST['emailOK'];
Upvotes: 1
Reputation: 134
let dict = ["emailOK" : "Hello"]
let jsonData = try! NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted)
let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding)! as String
let request = NSMutableURLRequest(URL: NSURL(string:"url")!)
request.HTTPMethod = "POST"
let postString = jsonString
request.HTTPBody = postString.dataUsingEncoding(NSASCIIStringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
}
Upvotes: 0
Reputation: 330
If you don't want to spend a lot of hours with useless efforts to write your successful get/post request, your best choice's to rely on libraries with all methods that you need.
In my humble opinion, I suggest you Alamofire https://github.com/Alamofire/Alamofire
Otherwise, to complete your request successfully you should specify Content-Type, Content-Type header information
Hope this helps you.
Upvotes: 0
Reputation: 1045
If you need to send the email as part of url, you should include it in URL:
var requestURL = url + "?" + postData
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
But since you are sending a POST request, it's usually sent in HTTP body. Quite common way is to send it in JSON format:
let dict = ["emailOK" : "Hello"]
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted)
} catch _ {
request.HTTPBody = nil
}
Upvotes: 0
Reputation: 2693
Try this code to create session object. I am not sure whether this will completely resolve your issue,but I think you should give it a try.
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
Upvotes: 0