In this video, learn how Alamofire allows for various HTTP method types. GET is the default, but you may specify that it use POST, PUT, DELETE, and others. For a GET request, query parameters are encoded in the query string—as part of the URL. For URL parameter coding for a GET request, Alamofire also handles collections like arrays and dictionaries.
- [Instructor] So we looked at doing a regular GET request using just a URL string with Alamofire but many times we have to be able to send parameters along with the GET request. So Alamofire.request allows us to send additional parameters. Typically the GET parameters are sent to the Alamofire.request in the form of a dictionary where the key is a string and the value is Any. We'll also send in a ParameterEncoding enum value and that's a protocol, and we'll look at that and other options as we go.
The typical URLEncoding enum values for a GET request would be none, default, and queryString, and we'll see that actually in the uses of them, they all amount to the same thing. So in Xcode, we'll open up our project from the previous video, where we're doing a GET request that just takes a URL, and we want to be able to add parameters to that call. So in our function, fetchURL on line 29, we'll add a second parameter called param and that takes a dictionary of String:Any.
So of course in our call up on line 26, we have to pass in a param and we'll just hard-code that value here with a key of category that's equal to Dance and a second value that takes keyword as the key and a value of ballet. So here's our parameters that we're going to add to our GET request as a query string, category equals Dance, keyword equals ballet.
We're passing that in to our fetchURL and now we need to pass that in to the request that we would create with Alamofire. So we're using the request call that takes our URL string which we know is a URLConvertible, and if we look at the full definition of this function, you see that it takes the URLConvertible which we're passing in, it takes a method that's defaulting to .get, it takes an optional for parameters, it has a default value for parameter encoding to default, and it optionally takes headers.
So all these other values aren't really required. Well, now we want to pass in the parameters. So we'll add in the parameter parameters and pass in our param value. All the other ones are still optional, so we don't have to pass those in. We also know that our response coming back from the server is still text, so we can leave the responseString handler. Also, we know that it's JSON text, so we can leave the responseJSON request handler.
So now if we execute it, we'll see that our request is still created the same with parameters and we'll see our responses are still valid. We can use the encoding parameter of default, which in our case is queryString. So once our view appears, our fetchURL is called with the URL string and our parameters. The request is created and the response is sent back and printed out like it was before, but notice it's much fewer responses now that we have some filtering parameters added to our query.
And there it is as JSON. So things to remember when you're passing in parameters to a Alamofire.request, is that the GET parameters are in form of a dictionary with the key as a string and the value as Any. The parameters is a protocol so that you can write classes that implement the parameters protocol and send those in to create your own parameters, and then also URLEncoding has multiple values: .default, .queryString, or none, and for a GET request, none allows you to just use the default which ultimately propagates to .queryString for a GET request.
- Fetching data
- Managing text, JSON, and binary data
- Downloading files to the file system
- Request and response handling with Alamofire
- Encoding parameters
- Uploading and downloading files
- Reporting upload and download progress