user2362696
user2362696

Reputation: 141

Setting Custom HTTP Headers in Alamofire in iOS 7 not working

I've tried to set the Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders with my custom HTTP Headers in iOS 7 but I have had no luck.

This works fine in iOS 8.

Does anyone have any suggestions?

Upvotes: 12

Views: 5470

Answers (4)

San
San

Reputation: 1796

Below is the full code which works for iOS 7 and iOS 8

let URL = NSURL(string: request.url!)
var mutableURLRequest = NSMutableURLRequest(URL: URL!)
mutableURLRequest.HTTPMethod = Alamofire.Method.GET.rawValue

// Adding headers
var defaultHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = defaultHeaders

// Adding parameters
let manager = Alamofire.Manager(configuration: configuration)
let urlReq  = ParameterEncoding.URL.encode(mutableURLRequest, parameters: request.params).0
aReq = manager.request(urlReq)
aReq!.responseJSON { (req, response, JSON, error) in }

More info : Alamofire Issues - GitHub

Upvotes: -1

Thor
Thor

Reputation: 407

I'm using the NSMutableURLRequest to set custom headers.

Check this example.

var request = NSMutableURLRequest(URL: NSURL(string: "http://example.com")!)
request.HTTPMethod = "POST"
request.setValue("<HEADER VALUE>", forHTTPHeaderField: "<HEADER FIELD>")

var parameter: NSDictionary = ["dimensions" :["product" : ["name" : "macpro", "price" : "350"]]]
 request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameter, options: nil, error: nil)

Alamofire.request(request).response { (request, response, result, error) -> Void in
                // handle response
     println("\(request) \t \(response) \t \(result) \t \(error) ")
}

Upvotes: 1

Antonio Romano
Antonio Romano

Reputation: 285

I get stuck with the same problem, at the end I come up with the wolfing solution. (I changed a little bit the originally library, so be carefull!)

I was always calling the method:

public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request

And I noticed the call to the method:

private func URLRequest(method: Method, URL: URLStringConvertible) -> NSURLRequest

Just inside this function i inserted one line of code:

mutableURLRequest.setValue(valueHeader, forHTTPHeaderField: keyHeader)

with the following result (just to be clear):

private func URLRequest(method: Method, URL: URLStringConvertible) -> NSURLRequest {
    let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URL.URLString)!)
    mutableURLRequest.HTTPMethod = method.rawValue

    if(contentHeader){
        mutableURLRequest.setValue(valueHeader, forHTTPHeaderField: keyHeader!)
    }

    return mutableURLRequest
}

I created also a method to pass the values. I hope this will work

Upvotes: 1

Andreas Du Rietz
Andreas Du Rietz

Reputation: 1171

I got it working.

This has no effect on iOS7:

Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["CustomHeader": customValue]

This however will work on both iOS7 & 8:

var headers = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
    headers["CustomHeader"] = customValue

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPAdditionalHeaders = headers

    Alamofire
        .Manager(configuration: configuration)
        .request(.GET, "https://example.com/api/users", parameters: nil, encoding: .JSON)

Upvotes: 7

Related Questions