Basic Alamofire Request Syntax

In this lesson

Here we will learn about the basic HTTP request method in Alamofire with a quick tour over its parameters, required and optional, as well as an example response block to the request!

Kyle Roberts
Swift Guru at Large

Kyle's Series


Tap on time to skip ahead


Hello world, Kyle here with and we’re going to talk about the basic URL HTTP request syntax in Alamofire. So in most of the instances that I’m using the Alamofire.request method for my standard HTTP requests, I am not utilizing all the parameters, but I am going to talk about them here. This first example, the actual method to create a request is with the Alamofire.request method. The current parameters I’m using are the HTTP method, the URL for the request, and a couple parameters that I’ve added. But if we Command+click into that method, you can actually see its definition in the Alamofire CocoaPod. And there’s a couple more parameters here that I’m not using back in my code.


The first parameter here is the method, and as we saw, we were using the Get HTTP method, and as it’s the first parameter in this function, the name of the parameter is not required when implementing it. So you see, we don’t have method: here. The second parameter is also an unnamed parameter as it is prefixed with this underscore here, of the type URLStringConvertible, and in all the cases of my NetworkInterface class here, I am passing in just standard Swift strings for that. And after that, you get into some more specific parameters that are named, but they do have defaults and their implementation is actually optional. But those are parameters which not method parameters but the actual request or URL parameters. And we see that it takes a dictionary type and the default value is nil so no parameters.


The next method parameter is encoding which is actually of the Alamofire enum type ParameterEncoding with a default of .URL. And you can also define some headers in this last headers parameter that is another dictionary type with a default value of nil, and of course it returns the Alamofire request object. 


So here, we can see back in the example in the SteamReader code, we are creating the request with the request method, specifying the HTTP method, the next unnamed parameter is of course the URL in the form of a String here, and then a named parameter called parameters for the actual HTTP parameters. And then after that, if we wanted a value other than the defaults that are defined in the Alamofire code, we can just add those here, I believe .JSON is another available encoding for the parameters. The final argument for this method is headers. And I’m just gonna make some bogus header here called header example. And then the text example


So that’s the example of creating the request using all of the available argument types and parameters that are defined in the request method. But in many cases you probably won’t be using them all like I am here. And at the very least, all you need are the first two, since all the other parameters have default values, you need to specify the HTTP method and then some form of a URL. 


So once you have your requests, we can see here that I’m actually storing it to a local variable or a local constant, all you need to do to run that request is call one of the response methods on it. And there are several response methods but in this case when using the response.JSON one but you can just start the response closure and have access to the response object included in that. And this whole block here within the closure is called asynchronously after the request has completed. We can access the response object just like that. And again we still have access to the original request. 


Another way to do this if I didn’t have some of this weird logic here is we could actually call the responseJSON method on the original request creation method. And here, we don’t have access to the original request, and then to actually get access to this request object, we can call response.request. And is actually a different object type than what we were encountering before, and the error to this is I’m expecting a different type in this method so I can just change this method’s initial argument type from whatever it is to the Alamofire request object type, which is just the class Request. But the syntax is pretty easy to run a request, and you see here we have most of the work in 3 lines, and 4 lines if you count the end of that closure there. And it’s pretty awesome, everything is handled asynchronously and it works great. 

Additional Info

Register to get access to additional resources and info.