I'm trying to do a POST consumption using Alamofire, my internet seems to be working well but I keep getting the error:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
However I do the consumption in Postman and it works perfectly.
This is how I do the consumption:
func sendRequest<T: Decodable>(endpoint: Endpoint, responseModel: T.Type) async -> Result<T, RequestError> {
var urlComponents = URLComponents()
urlComponents.scheme = endpoint.scheme
urlComponents.host = endpoint.host
urlComponents.path = endpoint.path
urlComponents.queryItems = endpoint.queryItems
print("Sending request to url \(urlComponents.url?.absoluteString ?? "")")
guard let url = urlComponents.url?.absoluteString.removingPercentEncoding else { return .failure(.invalidURL) }
do {
var urlRequest = try URLRequest(url: url.asURL())
urlRequest.httpMethod = endpoint.method.rawValue
urlRequest.allHTTPHeaderFields = endpoint.header
if let body = endpoint.body {
urlRequest.httpBody = body
}
let request = AF.request(urlRequest).serializingDecodable(responseModel)
guard let response = await request.response.response else {
return .failure(.noResponse)
}
print("* * * * * * * * * ")
urlRequest.printFullRequest()
print("* * * * * * * * * ")
print(await request.response)
print(await request.result)
print("JSON DATA: \(await String(data: request.response.data ?? Data(), encoding: .utf8) ?? "")")
switch response.statusCode {
case 200...299:
guard let decodedResponse = try? await request.value else {
return .failure(.decode)
}
return .success(decodedResponse)
case 401:
return .failure(.unauthorized)
default:
print("Response status code: \(response.statusCode)")
return .failure(.unexpectedStatusCode)
}
} catch {
return .failure(.unknown)
}
}
I've already turned the Arbitrary Loads on, already changed the WiFi, tried on mobile data, restarted the phone, erased the simulator, tried printing the full request and the headers and url construction seem completely correct. I know it's not my connection because I have stable 500 Mbps and again, in Postman works perfectly. What can I do?