Suddenly URLSession.shared.dataTask not working

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.

Do you receive an error message ? If so, which ?

Did it stop running on all 3 Mac ?

If the exact same code was running on all 3 Mac before with the same code

  • are you absolutely sure you did not change anything to the app ?
  • the issue is not in code.
  • It is unlikely to be in Macs configuration if problem occurs simultaneously on all 3
  • Is it your server or a third party server ? If yours, did anything change on the server ?
  • Did you change any configuration on your network (did the Internet router reboot for instance ?)

I would try, one after the other:

  • reboot server
  • reboot router
  • reboot Macs

Do you receive an error message ? If so, which ?

No

Did it stop running on all 3 Mac ?

Yes

If the exact same code was running on all 3 Mac before with the same code are you absolutely sure you did not change anything to the app ?

Yes

I tried all the above mentioned probabilities.

Same url is working with postman same url is working in windows system

More questions than answers so far…

.

      guard (200 ... 299) ~= response.statusCode else {          // check for http errors

So you don't get error here ?

What do you get in response and response.statusCode ?

What do you get for each print() in the code ?

Is it an https or http URL ?

Do all the 3 Mac share the same AppleID ? If so, could you try from another AppleID ?

So you don't get error here ?

Yes

What do you get in response and response.statusCode ?

200

What do you get for each print() in the code ?

responseString =

Is it an https or http URL ?

https

Do all the 3 Mac share the same AppleID ? If so, could you try from another AppleID ?

3 Mac sharing the same AppleID

We don't have another AppleID

Suddenly URLSession.shared.dataTask not working
 
 
Q