<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Farhan Almasyhur on Medium]]></title>
        <description><![CDATA[Stories by Farhan Almasyhur on Medium]]></description>
        <link>https://medium.com/@FarhanAlmasyhur?source=rss-e1813e88045a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aTlFhi5syOgbrEyo3oK0HQ.jpeg</url>
            <title>Stories by Farhan Almasyhur on Medium</title>
            <link>https://medium.com/@FarhanAlmasyhur?source=rss-e1813e88045a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 22:27:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@FarhanAlmasyhur/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[HTTP Methods with URLSession]]></title>
            <link>https://medium.com/@FarhanAlmasyhur/http-methods-with-urlsession-6a3cdb7d18c1?source=rss-e1813e88045a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a3cdb7d18c1</guid>
            <dc:creator><![CDATA[Farhan Almasyhur]]></dc:creator>
            <pubDate>Fri, 05 Aug 2022 02:47:25 GMT</pubDate>
            <atom:updated>2022-08-13T15:35:01.808Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, I will show you on how to do HTTP methods using URLSession for iOS Development.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*OsQDpDLQLgxxJT07zHf6pQ.jpeg" /></figure><p><strong>Why using URLSession?</strong></p><ul><li>URLSession was built on top of foundation in swift</li></ul><p>Because URLSession is built on top of foundation framework, it did not need any installation of other libraries. If you use other networking such as Alamofire, you need to install the dependencies first using CocoaPods or Swift Package Manager.</p><ul><li>Suitable for simple and complex use cases</li></ul><p>URLSession is already a stable network libraries made by Apple Developer Team. So, URLSession can be used even in simple application that has only one or two hits on API or complex application that may needs many API endpoints.</p><ul><li>Can directly use for combine</li></ul><p>Do you build your application using reactive programming using combine? if yes then URLSession might be the answer to help you in developing your application. By using URLSession, you can use <strong>URLSession.dataTaskPublisher</strong> instance method for publishing data if the task completed or terminates if task get an error.</p><p><strong>How to use URLSession</strong></p><ul><li>Check how to use request and response using API Platform</li></ul><p>The first step to know better about the API you going to use is using API Platform. One of the API Platform that “easy to use” is Postman. With Postman you can try the API endpoint and hit it with HTTP Method such as GET and POST.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ad_d8D3pnim0sob6n09ozw.png" /><figcaption>Hit leetcode API using query and variables key JSON</figcaption></figure><p>In this case, I try to hit <a href="https://leetcode.com/graphql">leetcode API</a> with body query and variables using JSON. With this body, we can make request to leetcode API URL with the query body we already defined. After the request is success, we can get the response based on the body we sent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*q1xthLe6aQOXAWHIlJ3a4A.png" /><figcaption>response data from leetcode</figcaption></figure><ul><li>Prepare the model</li></ul><p>The next step after we get the data from the response is to prepare the model for our app. Because we know the response is based on JSON object or JSON Array, so we can make our model directly. In example, key “data” has value of an object with key “problemsetQuestionList”. So, in our model data, we can make it like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*tVzi2tyZtRLrtojBDiHSgg.png" /></figure><p>The ProblemData struct and DataClass is only two of many model we going to build. All the struct in the data model will conform the protocol Codable. With the struct conform to Codable, we can decode the JSON data we get from the response. To help create all the data that we need from JSON, we can use <a href="https://app.quicktype.io/">quicktype</a> to generate all the model data based on the JSON response.</p><ul><li>Create Manager</li></ul><p>To help us reach <a href="https://www.raywenderlich.com/21503974-solid-principles-for-ios-apps"><strong>SOLID</strong></a> on <strong>Single Responsibility</strong>, we create a NetworkManager to have a single use to get data from the API. In the NetworkManager, we make a function to get the data with a callback function. We name the function “getProblems” because it will get the problems from LeetCode. This function needs a completion handler to return the data from this function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EOeidgfq4DssmHCWKFrGlw.png" /></figure><p>The first thing we need to define is the URL we going to hit with HTTP method. In this case, we are going to hit <a href="http://leetcode.com/graphql.">http://leetcode.com/graphql.</a> After we define the URL, we must define the HTTPMethod, Header-Value, and HTTPBody using URLRequest. After we done define the method, body, and headers, we can now create the task to hit the API.</p><p>In the request task, we use URLSession.shared.dataTask to use the request and hit the API. Inside the closure, we must decode the response with JSONDecoder to our data model. After we decode it, we can now give the response data to our completionhandler.</p><ul><li>Using the method</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G4cYz8i0PH-AfLcaIpBn7g.png" /></figure><p>When using the manager getProblems method, we must create completionHandler. After that, we can now save our data to database that we use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kxZmFJSMM4aP-Z7AMtE7IQ.png" /><figcaption>all the LeetCode problems that we get</figcaption></figure><p><strong>References</strong></p><ul><li><a href="https://cocoacasts.com/networking-fundamentals-how-to-make-an-http-request-in-swift">https://cocoacasts.com/networking-fundamentals-how-to-make-an-http-request-in-swift</a></li><li><a href="https://www.raywenderlich.com/3244963-urlsession-tutorial-getting-started">https://www.raywenderlich.com/3244963-urlsession-tutorial-getting-started</a></li></ul><p>© 2022 Muhammad Farhan Almasyhur. All Rights Reserved.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a3cdb7d18c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>