php - HTTPS POST working successfully but variables come up null -


the code i've posted here shortened versions essentials issue. when put through rigorous error testing, appears there no issues either within request, within server, or within return code, except fact response server says "function": null instead of "function": "adduser" should. there i'm doing wrong on either end? seems not recognizing posted variables, i've used code numerous times in other apps , servers , seems failing here reason cannot see. thank assistance.

here php on server:

<?php    $t = $_post["function"];    $do = array("success"=>true, "function"=> $t);    echo json_encode($do); ?> 

here swift i'm using make request:

  let params = ["function": "adduser"] [string: anyobject]?       fetchdata("https://pdt.pitchprogress.net/samplephp.php", token: nil, parameters: params, method: "post", oncompletion: { (success, data) -> void in         if success {             {                 let json = try json(data: data!)                 if json["success"].boolvalue == true {                     print("success!")                     print(json.description)                     print(json["function"].stringvalue)                }               }                    }                     })            func fetchdata(_ feed:string,token:string? = nil,parameters:[string:anyobject]? = nil,method:string? = nil, oncompletion:@escaping (_ success:bool,_ data:data?)->void){  dispatchqueue.main.async {       if let unwrapped_url = url(string: feed){         let request = nsmutableurlrequest(url: unwrapped_url)          if let parm = parameters {             if let data = (try? jsonserialization.data(withjsonobject: parm, options:[])) data? {                  request.httpbody = data                 request.setvalue("application/json", forhttpheaderfield: "content-type")                 request.setvalue("\(data.count)", forhttpheaderfield: "content-length")              }         }          if let unwrapped_method = method {             request.httpmethod = unwrapped_method         }          let sessionconfiguration = urlsessionconfiguration.default         sessionconfiguration.timeoutintervalforrequest = 15.0         let session = urlsession(configuration: sessionconfiguration)          let taskgetcategories = session.datatask(with: request urlrequest, completionhandler: { (responsedata, response, error) -> void in              let statuscode = (response as! httpurlresponse?)?.statuscode             //println("status code: \(statuscode), error: \(error)")             if error != nil || (statuscode != 200 && statuscode != 201 && statuscode != 202){                 oncompletion(false, nil)              } else {                 oncompletion(true, responsedata)             }         })          taskgetcategories.resume()     } } } 

this because sending params json while api requires params form data.

this code sends params simple form post , retrieved successful response.

var request = urlrequest(url: url(string: "http://app123.freeiz.com/apis/samples/api4.php")!) request.httpmethod = "post" var poststring =  "" poststring.append("function=value") // replace 'function' paramname , 'value' value' request.httpbody = poststring.data(using: .utf8)  let task = urlsession.shared.datatask(with: request) { data, response, error in         guard let data = data, error == nil else {             // check fundamental networking error             print("error=\(string(describing: error))")             return         }          if let httpstatus = response as? httpurlresponse, httpstatus.statuscode != 200 {             // check http errors             print("statuscode should 200, \(httpstatus.statuscode)")             print("response = \(string(describing: response))")         }          let responsestring = string(data: data, encoding: .utf8)          if let unwrappedresponsestring = responsestring{              print(unwrappedresponsestring)         }      }     task.resume() 

i had once made small wrapper sending simple form data, if find update answer that. till can try this. let me know how goes..


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -