Home > other >  I'm not receiving response from API when making a post request
I'm not receiving response from API when making a post request

Time:08-30

I'm not getting an exact response from API call as I have accurately posted the data and I using breakpoints I've figured out the error is at getting the response.

@IBAction func onTapContinue(_ sender: UIButton) {
    registerData["otp_code"] = codeTextField.text
    print("Registration Check \(registerData)")
   apiCall()
    
}

extension VerificationCodeVC {
    func apiCall (){
        guard let url = URL(string: "https://admin.sapid.mx/api/register") else {
            print("Error: cannot create URL")
            return
        }
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/jason", forHTTPHeaderField: "Content-Type")
       
        request.httpBody = try? JSONSerialization.data(withJSONObject:  registerData, options: .fragmentsAllowed)
        let task = URLSession.shared.dataTask(with: request) { data, _ ,error in
            guard let data = data , error == nil else {
                return
            }
                        do {
                let response = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
                print("success:\(response)")
            }
            catch {
                print(error)
            }
            
        }
        task.resume()
    }
}

CodePudding user response:

Use below line to set Content-Type, it should be application/json instead of application/jason.

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

Also check, It must have included NSAppTransportSecurity in info.plist file.

Transport security has blocked a cleartext HTTP

  • Related