-2

I am having trouble calling APIs with URLSession, but it works fine with AF.request. I used URLSession in all places where API calls were made, so I do not want to change with AF.

Code:

let parameters = [
    "fk_EmpGLCode": "\(fk_EmpGLCode)",
    "varClientName": "\(varClient)",
    "strAppType": "IOS","strVersion": "\(VERSION_NUMBER)","varIMEINumber":
"\(ECP_Constant.varIMEInumber)","UserName": "\(userName)","Filename":
"\(zipName).zip","FileArray":
"\(fileData1)","varAppType":"\(Constan
.varAppType)"
]

var request = URLRequest(url: URL(string: web)!)

request.setValue(SyncConstant.HeaderToken, forHTTPHeaderField: "varToken")

request.httpMethod = "POST"
var  jsonData1 = NSData()
do {
    jsonData1 = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) as
NSData
} catch let error as NSError {
    NSLog("delete directory \(error.debugDescription)")
}
request.httpBody = jsonData1 as Data
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 30.0
sessionConfig.timeoutIntervalForResource = 60.0
let session = URLSession(configuration: sessionConfig)

let task = session.dataTask(with: request) { (data, response, error) in
    if(data != nil) {
       
        if let httpResponse = response as? HTTPURLResponse {
            print("Status Code: \(httpResponse.statusCode)")
        }
        
        var jsonData = String(data: data!, encoding: String.Encoding.utf8) as String?

When I use URLSession to make an API call, I receive an error.

{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}

I tried with diffrent methods of API Call.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
  • Well, looking at your code, I can see that... oh... wait... silly me. – matt Sep 01 '23 at 12:39
  • @matt, I updated the question with the code. Please check and let me know if anything missing. – Sagar Zala Sep 01 '23 at 12:54
  • There is no parameter? What's the Alamofire working code? Did you need to add headers? Are the parameters in the URL? Did you add them correctly? – Larme Sep 01 '23 at 12:58
  • See here: https://stackoverflow.com/questions/13546429/ - it may help you figuring out what the exact error is. – koen Sep 01 '23 at 13:04
  • @Larme added parameters – Sagar Zala Sep 01 '23 at 13:04
  • 2
    It's hard to tell what's wrong if we don't know the working code nor the Web API documentation/needs. Why? Because your code is currently working: it's sending the request and get a response. The response is saying that's there is an error, but we can't guess which one. Would you mind sharing the working Alamofire code, because that's what you want to replicate. Unrelated but `NSData` -> `Data`, that's avoid you later the `as Data`, and the `catch let error as NSError` can just be `catch { print(""delete directory: \(error)" }`, are you using `NSLog` and not `print`? – Larme Sep 01 '23 at 13:07
  • 1
    The code will fail if one of the String Interpolated parameters is an optional. And – unrelated – don't `prettyPrint` JSON, the server doesn't care. And don't use `NSData` in Swift. And put all the *good* code into the `do` scope, it makes no sense to go on after `catch`ing an error. And delete `as String?`, it's redundant. – vadian Sep 01 '23 at 14:50
  • Does this answer your question? [How to make HTTP Post request with JSON body in Swift?](https://stackoverflow.com/questions/31937686/how-to-make-http-post-request-with-json-body-in-swift) – workingdog support Ukraine Sep 02 '23 at 06:22

0 Answers0