1

The incoming JSON from MockURL200Session produces a value of 2017-11-25 09:47:18 Etc/GMT.

class MockURL200Session: URLSessionProtocol {
    var nextDataTask = MockURLSessionDataTask()
    var nextData: Data?
    var nextError: Error?

    func httpURLResponse(request: NSURLRequest) -> URLResponse {
        return HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)!
    }

    func dataTask(with request: NSURLRequest, completionHandler: @escaping DataTaskResult) -> URLSessionDataTaskProtocol {
        let json = ["expires_date": "2017-11-25 09:47:18 Etc/GMT"]
        nextData = try! JSONSerialization.data(withJSONObject: json, options: [])
        completionHandler(nextData, httpURLResponse(request: request), nextError)
        return nextDataTask as URLSessionDataTaskProtocol
    }
}

However, once it arrives on the client side the forward slash simply disappears and becomes 2017-11-25 09:47:18 Etc GMT

self.httpClient.post(url: url) { (data, response, error) in
   let json = try! JSONSerialization.jsonObject(with: data!, options: []) as! Dictionary<String, Any>
}

How can I prevent it or escape it?

Houman
  • 64,245
  • 87
  • 278
  • 460
  • It seems that the issue happens server-side, since if you decode the encoded JSON data using `JSONSerialization`, the output is correct. If you send the same request from Postman, do you receive a correct response? – Dávid Pásztor Mar 15 '18 at 20:43
  • Server side works actually ok. It's the mock, that doesn't work. – Houman Mar 15 '18 at 20:52

1 Answers1

1

In dataTask(), your call to JSONSerialization.data() is escaping that forward slash when it decodes. You can see it in nextData before you return from dataTask(). Somewhere on its journey back to the client side, the \/ combination is being removed. I would guess it's when you try to decode the server response. Now that you know what's happening, you can find the solution to the rest of your problem here, or if not, google something like "swift jsonserialization escape slash". Best of luck to you.

SaganRitual
  • 3,143
  • 2
  • 24
  • 40