Alamofire: Where are "defaultHTTPHeaders"?

Created on 18 Feb 2019  ·  8Comments  ·  Source: Alamofire/Alamofire

What did you do?

Updated Alamofire from 4.x to 5.0.0 beta 2

What did you expect to happen?

Everything works

What happened instead?

There were SessionManager.defaultHTTPHeaders. Now it doesn't exist and there is no info what and how to use instead. Of course I can't even compile the project

Alamofire Environment

Alamofire version: 5.0.0 beta 2
Xcode version: 10.1
Swift version: 4
Platform(s) running Alamofire: iOS
macOS version running Xcode: 10.14.3

support

Most helpful comment

Alamofire 5 now has an HTTPHeaders type, which has a default static property for the the default headers.

All 8 comments

Alamofire 5 now has an HTTPHeaders type, which has a default static property for the the default headers.

@jshier I have tried like the one below but it did not work.Can you please help?configuration.httpAdditionalHeaders = HTTPHeaders.default

configuration.headers = .default // not httpAdditionalHeaders, it's Alamofire 4

You can also use HTTPHeaders.default.dictionary if you want to get dictionary of default headers as it was in Alamofire 4.

You can also start with the Alamofire URLSessionConfiguration and not have to add our headers at all:

let configuration = URLSessionConfiguration.af.default

This configuration is the same as URLSessionConfiguration.default, but also includes our default headers.

Otherwise, we also have an extension on URLSessionConfiguration to work with Alamofire's HTTPHeaders type:

let configuration = URLSessionConfiguration.default
configuration.headers = .default

Swift 5 with Alamofire 5:

var headers = AF.session.configuration.headers

That's incorrect: you should use HTTPHeaders.default if you want our default set of headers, or URLSessionConfiguration.af.default if you want a URLSessionConfiguration with our defaults headers already added.

In my case I replaced SessionManager.defaultHTTPHeaders["User-Agent"] by HTTPHeader.defaultUserAgent.value.

Was this page helpful?
0 / 5 - 0 ratings