HTTP Headers in Alamofire

In this lesson

Defining HTTP headers for an Alamofire request is as easy as peasy and will take you no time at all. Really the hard part of this should be listed in the documentation for whatever web service you’re using, or at least given to you by the API dev!

Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

0:11

Hello world, Kyle here with brax.tv and we’re going to talk about defining HTTP headers when making an Alamofire request. And doing that is as easy as adding the headers argument to the request initialization method. And it accepts a Swift dictionary type with key and value types of String. And here’s just an example here of the accepted return type HTTP header, making sure that the, or just verifying that the HTTP requests is returning some JSON. And it’s as simple as that to add HTTP headers to a request.

0:50

Most of the time, the required headers are going to be defined somewhere in the REST, and not necessarily REST but somewhere in the web API that you’re using. And this case of the Steam API, for my app SteamReader, no HTTP headers are explicitly required, which is alright. The default value for this argument is nil since there are no headers required, we don’t actually have to include this parameter, this argument, at all. But when you do, it’s as easy as this. And of course sometimes you do run into an API of it once tons and tons of headers defined, which is just fine. And if you don’t want to clog up your clean code down here, then you can actually move that to a separately defined constant and then just pass it in. But it’s as easy as that. 

Additional Info

Register to get access to additional resources and info.