Home > Enterprise >  URLSession not sending data to PHP API
URLSession not sending data to PHP API

Time:06-10

I am using this code below to send a HTTP request to my PHP API

static func submitNumber(parameters: [String: Any]){
    print("parameters", parameters)
    guard let url = URL(string: Constants.phoneVerifyUrl) else {
        print("URL not found")
        return
    }
    let datas = try! JSONSerialization.data(withJSONObject: parameters, options: [])
    
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.httpBody = datas
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    
    let urlSession = URLSession.shared.dataTask(with: request) { data, _, error in
        if error != nil {
            print("error", error?.localizedDescription ?? "")
            return
        }
        do {
            if let data = data {
                print("data", data)
                let decodedData = try JSONDecoder().decode(DataModels.self, from: data)
                DispatchQueue.main.async {
                    let noError = decodedData.noError
                    let dataStr = decodedData.dataStr
                    print("noError", noError)
                    print("dataStr", dataStr)
                }
            } else {
                print("No Data received")
            }
        } catch let JsonError {
            print("JSON error", JsonError.localizedDescription)
        }
    }
    urlSession.resume()
}

And then in my PHP API, I try to receive the data like this

<?php
    if(isset($_POST)){
        $phoneNumber = $_POST['phoneNumber'];
        
        //Run Code here
        
    }
?>

When I do

print("parameters", parameters)

This is what I get (As expected)

parameters ["phoneNumber": "1234567890"]

But then, for a reason I don't know, the code in my PHP API is not executing. The PHP code is perfectly working because I use the same API for my Android app and it works fine, so I know the issue is not from my PHP API

And when I also do this print("data", data) I get a random number like this data 8402 bytes

For me, I believe I'm not passing the parameters in the right way to my API, Since I'm new to Swift, I don't know how it's done

And for my URL string Constants.phoneVerifyUrl, it's okay

Please note: I don't want to temper with my PHP API as I also use it for my Android app. So I need only to fix my Swift code

CodePudding user response:

I found an answer from to create post body request data using below code.

var jsonData:Data?
do {
   jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
} catch {
  print(error.localizedDescription)
}

and then create the request like this.

let url = URL(string: "https://blah.com/server/dudes/decide/this")!
var request = URLRequest(url: url)

request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept") 
request.httpMethod = "POST"
request.httpBody = jsonData

CodePudding user response:

Okay... My first answer was wrong, but I've got it figured out.

First I removed this lines

request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")

And replaced it with this

request.addValue("application/json", forHTTPHeaderField: "Accept")

Then I changed my Data parameters dictionary from JSON to String

var datas = parameters.toQueryString.data(using: .utf8)!

Below is the Dictionary extension of toQueryString

extension Dictionary {
    var toQueryString: String? {
        return self.reduce("") { "\($0!)\($1.0)=\($1.1)&" }
    }
}
  • Related