ios – Alamofire stacking responses

[ad_1]

I’m utilizing Alamofire to speaking with my API, additionally I’m utilizing RequestInterceptor to catch unauthorized requests or for refreshing the JWT token. When every little thing goes properly, there is no such thing as a downside in any respect, however in case of unhealthy response (400,401) Alamofire tries to refresh token and ship the request once more. There’s a unusual conduct, because it fails with serialization error:

[Request]: PUT https://my.url.com/unregistered?
    [Headers]:
        Content material-Kind: software/json
    [Body]:
        {"identify":"user16","electronic mail":"[email protected]"}
[Response]:
    [Status Code]: 400
    [Headers]:
        Connection: keep-alive
        Content material-Size: 51
        Content material-Kind: software/json
        Date: Solar, 19 Sep 2021 08:13:57 GMT
        Server: nginx
    [Body]:
        {"message": "Person with identical electronic mail already exists"}
        {"message": "Person with identical electronic mail already exists"}
[Network Duration]: 0.21983695030212402s
[Serialization Duration]: 0.0001439583720639348s
[Result]: failure(Alamofire.AFError.responseSerializationFailed(purpose: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given information was not legitimate JSON.", underlyingError: Elective(Error Area=NSCocoaErrorDomain Code=3840 "Rubbish at finish." UserInfo={NSDebugDescription=Rubbish at finish.}))))))

It principally learn the identical reply two occasions and inform me that it isn’t a JSON serializable. I’m completely positive that server returns just one response like JSON, what is completely unusual is proven fact that Alamofire doing this at any time, as soon as it get again with efficiently serialized physique and second time it fails on serialization. That is the code I’m utilizing:

func authorization<T: Decodable>(
        _ url: String,
        methodology: HTTPMethod = .get,
        parameters: Parameters? = nil,
        decoder: JSONDecoder = JSONDecoder(),
        headers: HTTPHeaders? = nil,
        interceptor: RequestInterceptor? = nil,
        withInterceptor: Bool = false
    ) -> Future<T, ServerError> {
        return Future({ promise in
            AF.request(
                url,
                methodology: methodology,
                parameters: parameters,
                encoding: JSONEncoding.default,
                headers: headers,
                interceptor: withInterceptor ? self : nil
            )
            .validate(statusCode: [200, 401])
            .responseDecodable(completionHandler: { (response: DataResponse<T, AFError>) in
                print(response.debugDescription)
                change response.end result {
                case .success(let worth):
                    self.saveCookies(cookies: HTTPCookieStorage.shared.cookies)
                    promise(.success(worth))
                case .failure(let error):
                    promise(.failure(self.createError(response: response.response, AFerror: error, AFIerror: nil, information: response.information)))
                }
            })
        })
    } 

Additionally, I’ve tried including a number of standing codes to validation, nothing helps in any respect.

Once I’ve used .responseString to get any ideas I used to be in a position to see the response solely as soon as, which is unusual too.

[ad_2]

Leave a Comment