XCode API Usage [POST Data from API] - SESSION V

 

Instead of making one long video I split the important topics “XCODE API usage for App Development “into two topics. In the last XCODE learning lab [Session IV], we learnt how we can communicate with API’s to get data. In this XCODE learning lab [Session V] we are gonna look at how we can communicate with websites to post some data in the process of POST Requests. We shall post some data to a website and will get a response if it was successful or not. Typical example: Online booking of Movie ticket or Charging of Mobile phone talk time. So, it’s a very important aspect of App Development.

 
 
 
Video file

 

This API functionality is the most critical aspect in your App development due to its significance to connect your app to external as well as internal databases.

 

I have already talked about what an API is last session, so feel free to watch that for a quick recap.

 

Image removed.

 

The POST request method requests that a web server accept the data enclosed in the body of the request message, most likely for storing it.

 

You also can’t just post anything that you want to an API. It has specific guidelines, like it did in get requests on what to put in the url, and how to get it. For today’s project, we are going to use a sample POST data website.

 

Let me give you a demo of what we are going to build today. So, we post this body to a placeholder json website and get a response back with an id for what we’ve posted.

 

We shall be creating this using: –

 

  • Functions (To hold code)
  • URLSession (For POSTing data)
  • Setting the request type to POST
  • Creating a body (for the request)

 

So go ahead and open up Xcode and create a new playground. We are not making an app for this because this project is very simple and can easily be incorporated in a playground.

 

Image removed.

 

Now let’s look at what we covered today: –

 

  • URL Session request
  • Setting the request type to POST
  • Creating the body for the request
  • Printing out the returned JSON in JSON format
  • The solution for Session IV’s challenge.

 

 

Thank you for watching, in my next learning lab I will come back with more interesting and divergent topics. I am sure it will be a great and enriching learning experience for you. Let's innovate, challenge & broaden our intellectual horizons, and make it an interesting and joyous learning experience.

 

Till then, let's enjoy every moment! 🙂 🙂