0

I am working on a swiftui project where I need to post call a string of a specific date format to an api and retrieve response back from it.

code as follows:

func createProfile(birthday: String, email: String, fullName: String, gender: String, completion: @escaping (Result<Bool, NetworkError>) -> Void) {
    
    guard let url = URL(string: "*******") else {
        completion(.failure(.invalidURL))
        return
    }
    
    var request = URLRequest(url: url)
    let defaults = UserDefaults.standard
    guard let token = defaults.string(forKey: "jsonwebtoken") else {
        return
    }
    request.httpMethod = "POST"
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    let body: [String: String] = [
        "birthday" : "02/01/2022",
        "email": email,
        "fullName": fullName,
        "gender": gender
    ]
    request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
    print(body)
    
    URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            completion(.failure(.noData))
            return
        }
        guard let profile = try? JSONDecoder().decode(ProfileResponse.self, from: data) else {
            completion(.failure(.decodingError))
            return
        }
        print(profile)
        completion(.success(profile.activationStatus))
        
        
        
    }.resume()
}

with no loggings the code just doesn't work and the app runs normally, with the next print statements:

print(String(data: data, encoding: .utf8))

the console logs:

Optional("{\"errors\":{\"message\":\"parsing time \\\"\\\\\\\"02\\\\\\\\/01\\\\\\\\/2022\\\\\\\"\\\" as \\\"\\\\\\\"02/01/2006\\\\\\\"\\\": cannot parse \\\"\\\\\\\\/01\\\\\\\\/2022\\\\\\\"\\\" as \\\"/\\\"\"}}\n")

also when printing the request's body it is as expected:

["birthday": "02/01/2022", "fullName": "Samy", "email": "hello.world@gmail.com", "gender": "Male"]
DziKo
  • 13
  • 3
  • Can you include your definition for ProfileResponse? – ChrisTech44 May 31 '22 at 21:51
  • This seems to be an error returned from your backend in a response body, perhaps it's expecting a date in a different format. – Vadim Belyaev May 31 '22 at 21:55
  • Unrelated but: `.fragmentsAllowed` is not needed. The issue is that it will add a backslash before slashes in a String. It's valid JSON, but it seems to be an issue with that with your server parsing. `print(body)` is misleading, what you want to see is the JSON sent: `print(String(data: request.httpBody!, encoding: .utf8)!)` See https://stackoverflow.com/questions/47076329/swift-string-escaping-when-serializing-to-json-using-codable with Codable, but limited to iOS13+ it seems. But you might have a hint/lead to search... – Larme Jun 01 '22 at 05:53
  • does your server expect an array such as you show: `["birthday": "02/01/2022", "fullName": "Samy", "email": "hello.world@gmail.com", "gender": "Male"] ` or does it expect a json object, such as: `{ "birthday": "02/01/2022", "email": "hello.world@gmail.com", "fullName": "Samy", "gender": "Male"}` – workingdog support Ukraine Jun 01 '22 at 05:58

1 Answers1

0

@Larme 's answer and link suggestion helped me fix my code error, all the thanks to him: Unrelated but: .fragmentsAllowed is not needed. The issue is that it will add a backslash before slashes in a String. It's valid JSON, but it seems to be an issue with that with your server parsing. print(body) is misleading, what you want to see is the JSON sent:

print(String(data: request.httpBody!, encoding: .utf8)!)

See stackoverflow.com/questions/47076329 with Codable, but limited to iOS13+ it seems. But you might have a hint/lead to search...

missionMan
  • 873
  • 8
  • 21
DziKo
  • 13
  • 3