A couple lessons back we've talked about how their various types of request that
you can make to the server.
And so far, we've talk specifically about get request and
then have seen how to use them in our examples.
Earlier in this lesson, we mentioned that we'd also be implementing what's
called the post request to add a feature to our my favorite movies app.
Now, it's time to look at this in more detail.
A POST request, as you may recall is a request where the client
submits data to a web server for processing.
The data that's sent is enclosed in the message body of the request,
which can then be processed to a specified resource.
We'll use a post request to favorite or
unfavorite a movie in the Favorite Movies list of our app.
So let's go back to the movie database's API documentation.
By searching for the word favorite on the API page, I found this method,
which allows me to add or
remove a movie to an account's favorite list, which seems like what we want.
Now we'll click on the post banner to open up the right-hand panel,
which has information on how to set up this request.
Here at the top, we see the URL that the POST request should be made to.
In other words, that's where the resource we want to update is located.
Now, this section underneath that is titled request should give us
the information that we need to create this in code.
These little drop down menus give us a few additional options.
Let's keep this setting as production and this one appears to be
a language setting, so let's set it to Swift and see what we get.
Cool, this looks like stuff we can use even if we don't know exactly what
it is yet.
But before we dig into it, I feel like we missed something.
What was this over here at the top before we changed to Swift?
Headers, we haven't talked about that yet.
So headers are actually a part of an HTTP request along with the URL and
the message body, they allow us to supply additional parameters that
specify how the request should operate.
Here we see that for this request, two headers are supplied.
The accept header field allows us to specify which content types will accept
in response.
In our case, we want to accept JSON as our response.
The content type header field let's us specify what kind of content we're
sending in the body.
In other words, what format our data is in,
so that the server knows what's coming and can determine how to parse it.
There are many other types of HTTP header fields.
A list of them is linked in the instructor notes.
Awesome.
Now, we understand what these headers are for.
Let's go back to this code and see if we can understand what's happening.
So this already looks a bit familiar, we did this when making our other requests.
Here, we are creating the URL and
then using NSMuteableURLRequest to make the request from the URL.
Now these lines are new, but if you look at them,
we already know what they're doing.
Request.HTTPMethod = POST,
we're setting our request method type to be of type post,
then request.addvalue application/json for HTTP header field accept.
This is just the code we need to set up our two header fields.
Now if we look back at what was displayed when we had the language
settings set to raw, there was what looked to be a bit of JSON.
"Media_type", "movie", "media_id", 550, "favorite", true.
This seems like the information we'd want to give the server, but
with our own values specified.
So it looks like this little JSON piece is here to show us what we should
be sending.
Let's look back at the Swift code we're provided.
Request.HTTPBody =.
So it looks like here's where we want to supply that data,
the data that we want to post.
This is where we should fill in what movie we're talking about and
whether we want favorite to be true or not.
Those can be variables that we can put in our code.
The rest of this is actually all stuff we've seen before.
Just getting the sessions, making the task, etc.
We already know how to do that.
So when you make this POST request in your app in Xcode,
you should be able to copy over most of the spoiler plate code and
only really need to change a few things.