1

I want to post some data to my php page, I've tested two ways to do so but non of them worked :

let parameters = [
        "name": "test",
    ]

    Alamofire.request(URL(string: "http://www.tameshkshop.ir/11.php")!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { (response) in
        print(response.request)  // original URL request
        print(response.response) // URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization


        let responseString = String(data: response.data!, encoding: .utf8)

        print(responseString)

    }

the second way :

var request = URLRequest(url: URL(string: "http://www.tameshkshop.ir/11.php")!)
    request.httpMethod = "POST"
    let postString = "name=\(FinallFactorViewController.name)"        
    request.httpBody = postString.data(using: .)
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {                                                 // check for fundamental networking error
            print("error=\(error)")
            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = String(data: data, encoding: .utf8)

        let index = responseString?.index((responseString?.startIndex)!, offsetBy: 4)
       let done = responseString?.substring(to: index!)

in the php page, I get the posted value and echo them like this :

echo     $_POST['name'];

but it I get nothing in return .

what is wrong ? where am I doing is wrong ?

navid joe
  • 59
  • 6

1 Answers1

0

Your POST service is waiting for for httpBody parameters, not JSON. I did check this on postman. Use the following code to make your request:

let parameters = ["name":"Name"]
        Alamofire.request(.POST, "http://tameshkshop.ir/11.php", parameters: parameters)
            .responseString(completionHandler: { response in
                print(response.result.value)
            })

The output:

"test : Name |"

EDIT: You can't make a request using HTTP since iOS 9. Instead use HTTPS or Allow arbitrary loads in your .plist https://stackoverflow.com/a/33712228/5006492 Before going to production disable Allow arbitrary loads, is a security risk use HTTP. Get a SSL certificate for your site.

Community
  • 1
  • 1
  • thank you for your reply , Do I need any permission for posting data ? I just tested your code but It didn't work – navid joe Jan 24 '17 at 09:12
  • @PedroRothenCuevas If you can, always try to begin development with SSL it's a good habit to get into especially now SSL certificates are so cheap or even free at [Lets Encrypt](https://letsencrypt.org/) – Leon Storey Jan 24 '17 at 14:26
  • @LeonStorey Sure! Before going to production disable **Allow arbitrary loads**. Get a SSL certificate – Pedro Rothen Cuevas Jan 24 '17 at 14:42