3️⃣POST Requests
Fall 2023 | Vin Bui
Creating a POST Request
Inside of our NetworkManager
class, we write functions to be called by our application. These functions will use Alamofire to send HTTP requests.
Assume there is a struct called Member
that represents an AppDev member.
We can write the following code to add a member to the AppDev roster store in the backend.
Let’s break down this code.
Create a function called
fetchRoster
that takes in a callback (completion handler) as an argument. This callback takes in aMember
object. Note that this depends on what this POST request returns from the backend. However, we can expect the backend to return the member that we just added back to us.Specify the endpoint which is the URL that we will call to fetch the data. We can test this with Postman.
Define the request body. This POST request takes in three fields inside of the request body. Using the
member
parameter storing theMember
object that we passed in, we give values to these fields. Note that we do not necessarily need to have thismember
parameter. If we want, we can have three separate arguments passed into the function instead of one.Create a decoder to decode the data. If our object contains a
Date
property, then we need to specify the date decoding strategy. If the JSON contains fields with snake_case, then we also need to specify the key decoding strategy.Create the request using Alamofire.
Pass in the endpoint and specify the method (
.get
for GET,.post
for POST, etc).Encode the request with JSON to be sent over the internet (POST).
Validate the request to ensure that the status code is 2xx and if the content type matches.
Decode the response to
Member
using the decoder we created in Step 4.
Perform a switch statement on the response’s result.
If successful, pass to the callback the decoded response. A print statement is optional but recommended.
If failed, print an error statement about the error.
Note that we do not have to create the decoder inside of the function call. We can create a JSONDecoder
object and store it as a property in our NetworkManager
class to be used for all functions. The same can be applied to the endpoint variable containing a String value of our endpoint.
Let’s point out the differences between this and a GET request.
The function header usually contains a parameter storing some value that we want to send to the backend. In this case, we had a parameter called
member
storing aMember
object.We defined a dictionary whose type is
Parameters
. Remember, these are key-value pairs.Our method is
.post
instead of.get
. We also encode it usingJSONEncoding.default
.We pass in
parameters
to theAF.request
function. In the GET request version, we did not do this.
Calling the Network Request
Or if we plan on using self
somewhere,
This is exactly the same as it was with a GET request except that we have to pass in an argument to the function we created.
Success or Failure?
In the code samples above, our callback took in a Member
object. Sometimes the backend does not return this information, or we may not even need this information. Instead, it may be more useful for the callback to hold a more useful variable type: a Bool
.
If we were to refactor our code, it would look something like this:
The main difference here is that the callback takes in a Bool
instead of the Member
object. If the response succeeds, we use completion(true)
to pass in true
to the callback. If the response fails, we use completion(false)
.
When we call this function, we use success
to access the value passed to the callback. We can then do whatever we need depending on the status of the network request.
Last updated