Alamofire checks headers before receiving all data (full body)

Using Alamofire, is it possible to have a function to handle the response of the header before downloading the full file?

For instance:

Our application uses the same elements on several pages. These items are collected using the query. Each request has its own hash (md5 checksum). We send this hash in the headers, and I want to abort the request if the hash is recognized in the cache system.

Implementation example

APIManager.sharedManager.request(url, method: method, parameters: parameters)

            .doSomethingHere {
             //I want to read the headers here, before the data is fetched from the server.
             //There needs to be an option here to cancel the request.
            }

            .responseJSON { response in
             //If the request isn't cancel in the function above. The data should be here.
            }
}

Edit: solution (implementation of Alamofire SWIFT 3)

APIManager.sharedManager.delegate.dataTaskDidReceiveResponse =
        {(session:URLSession, dataTask:URLSessionDataTask, response:URLResponse) -> URLSession.ResponseDisposition in

            if let httpResponse = response as? HTTPURLResponse {
                //Do something with headers here. If you don't want to continue the request:
                return URLSession.ResponseDisposition.cancel

            }       

            return URLSession.ResponseDisposition.allow
    }

APIManager.sharedManager.request(url, method: method, parameters: parameters)
                .responseJSON { response in
                 //Response contains no data if it was canceled. 
                }
    }
+4
source share
2 answers

The title is actually part of the answer, so for this you may need to make two queries.

, , . . , .

:

  • .
  • ,
  • ( ), , , .

:

, ... urlSession(_:dataTask:didReceive:completionHandler:) URLSessionDataDelegate.

, . , , . .

2:

, HTTP/2 Server Push, , .

"push promises" . promises , , . , .

:

+2

, HTTP

Alamofire.request(urlString)
    .responseString { response in
        print("Success: \(response.result.isSuccess)")
        print("Response String: \(response.result.value)")

        var statusCode = response.response?.statusCode
        if let error = response.result.error as? AFError {  
            statusCode = error._code // statusCode private                 
            switch error {
            case .invalidURL(let url):
                print("Invalid URL: \(url) - \(error.localizedDescription)")
            case .parameterEncodingFailed(let reason):
                print("Parameter encoding failed: \(error.localizedDescription)")
                print("Failure Reason: \(reason)")
            case .multipartEncodingFailed(let reason):
                print("Multipart encoding failed: \(error.localizedDescription)")
                print("Failure Reason: \(reason)")
            case .responseValidationFailed(let reason):
                print("Response validation failed: \(error.localizedDescription)")
                print("Failure Reason: \(reason)")

                switch reason {
                case .dataFileNil, .dataFileReadFailed:
                    print("Downloaded file could not be read")
                case .missingContentType(let acceptableContentTypes):
                    print("Content Type Missing: \(acceptableContentTypes)")
                case .unacceptableContentType(let acceptableContentTypes, let responseContentType):
                    print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
                case .unacceptableStatusCode(let code):
                    print("Response status code was unacceptable: \(code)")
                    statusCode = code
                }
            case .responseSerializationFailed(let reason):
                print("Response serialization failed: \(error.localizedDescription)")
                print("Failure Reason: \(reason)")
                // statusCode = 3840 ???? maybe..
            }

            print("Underlying error: \(error.underlyingError)")
        } else if let error = response.result.error as? URLError {
            print("URLError occurred: \(error)")
        } else {
            print("Unknown error: \(response.result.error)")
        }

        print(statusCode) // the status code
}
-2

Source: https://habr.com/ru/post/1686887/


All Articles