I try post string request but its not working my code

Viewed 61

ı try to send string request but its not work

    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    let parameters = "{\"Language\": \"tr\",\"ProcessType\": 1,\"Username\": \"\(self.mailTextField.text ?? "")\",\"Password\": \"\(self.passwordTextField.text ?? "")\"}"
    print(parameters)
    let enUrlParams = try! parameters.aesEncrypt(key: LoginConstants.xApiKey, iv: LoginConstants.IV)
    print(enUrlParams)
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("*/*", forHTTPHeaderField: "Accept")
    request.httpBody = enUrlParams.data(using: .utf8)

Output:

{"Language": "tr","ProcessType": 1,"Username": "","Password": ""}

l+Au1MhqAlHr+wDR9UmdjN4IL5XHVnwMJx3rHF/P1MT+aO5Q5YF25f5OJRwDVzXEWu47ocqMxcUqw1onYBya9VCEvqjNQ0FpNCxtPp9fh+Y=

Optional(108 bytes)

statusCode should be 200, but is 500 response = Optional(<NSHTTPURLResponse: 0x600000e36e00> { URL: "my url" } { Status Code: 500, Headers { Date = ( "Wed, 14 Sep 2022 11:54:35 GMT" ); Server = ( "Microsoft-IIS/10.0" ); "Transfer-Encoding" = ( Identity ); "X-Powered-By" = ( "ASP.NET" ); } }) responseString = Optional("")

2 Answers

If you are getting 500 as HTTP Response Code, then you need to check with the API Team, as 500 is Server Not Available. If, Status Code lies in 4xx, then you need to bother about code, For 5xx you Server /API is not responding to your request.

I solved problem let stringRequest = ""(enUrlParams)"" this part solved my problem

    let url = URL(string: MemberUrl)!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    let parameters = "{\"Language\": \"tr\",\"ProcessType\": 1,\"Username\": \"\(self.mailTextField.text ?? "")\",\"Password\": \"\(self.passwordTextField.text ?? "")\"}"
    print(parameters)
    let enUrlParams = try! parameters.aesEncrypt(key: LoginConstants.xApiKey, iv: LoginConstants.IV)
    print(enUrlParams)
    let stringRequest = "\"\(enUrlParams)\""
    print(stringRequest)
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.httpBody = stringRequest.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
        guard let data = data, error == nil else {
            print("error=\(String(describing: error))")
            return
        }
        
        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(String(describing: response))")
            
        }
        if let responseString = String(data: data, encoding: .utf8) {
            print("responseString = \(String(describing: responseString))")
            self.userDC = responseString ?? ""
            self.userDC = try! self.aesDecrypt(key: LoginConstants.xApiKey, iv: LoginConstants.IV)
            print(self.userDC)
            self.login()
        }
    })
    task.resume()
Related