Hi,
I am new to MacOS SwiftUI App Development. I am having a post request implemented with URLSession.shared.dataTask
which it was working fine upto now. Suddenly i am not getting response from the request.
I am testing this parallelly in 3 macs, in that one mac is running continuously from past one week, another one is running is running from one day and last one is started running one hour before. All of sudden this request is stopped working at the time of post. my expected response is string added
let url = URL(string: "my url for request")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let encoded = try? JSONEncoder().encode("my data to post")
request.httpBody = encoded
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard
let data = data,
let response = response as? HTTPURLResponse,
error == nil
else {
// check for fundamental networking error
print("error", error ?? URLError(.badServerResponse))
return
}
guard (200 ... 299) ~= response.statusCode else { // check for http errors
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
do {
let responseObject = try JSONDecoder().decode(String.self, from: data)
print(responseObject)
} catch {
print(error) // parsing error
if let responseString = String(data: data, encoding: .utf8) {
print("responseString = \(responseString)")
} else {
print("unable to parse response as string")
}
}
}
task.resume()
Note: 1. only above mentioned post request stooped working, get requests are working fine. 2. I checked same post request with postman working fine. 3. i Checked same post request in windows system working fine.