iosswiftafnetworkingafnetworking-3

Use custom HTTP Headers with AFNetworking 3.0


I want to use my http header If-None-Match but I don't find how with AFNetworking 3.0

The Migration Guide doesn't explain anything about that and basically just say to use a normal GET request which doesn't seems helping for me.

Here's what I was doing with AFNetworking 2:

let getAvatarRequest = NSURLRequest(URL: avatarUrl!, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 60).mutableCopy()    
getAvatarRequest.setValue(avatarImageETag!, forHTTPHeaderField: "If-None-Match")

Thanks for your help.


Solution

  • Ok I found the answer, nothing actually changed, but it's not obvious, here's the code:

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    let manager = AFURLSessionManager(sessionConfiguration: configuration)
    
    let request = NSMutableURLRequest(URL: remoteFileURL!)
    request.setValue("", forHTTPHeaderField: "If-None-Match")
    let downloadTask = manager.downloadTaskWithRequest(request, progress: nil, destination: { (targetURL: NSURL, response: NSURLResponse) -> NSURL in
        // Success
    }, completionHandler: { (response: NSURLResponse, filePath: NSURL?, error: NSError?) -> Void in
        // Error
    })
    downloadTask.resume()