Reqeusty is an application that makes an api call to localhost:5001 and receives a json dictionary with two elements: item1 and item2.


The point of this application to make a simple api request, receive the response and do something with the response in the UI.


API in localhost

I am using Flask Framework to server the API and it seems running the default port 5000 was not working. I’m not sure if it’s a Swift thing about 5000 or maybe Requesty was also somehow running on 5000. But simply running on port 5001 made the difference.


Escaping Closure

The key here is to break the request (call and receiving of response) and processing the response into two functions. This helps to understand what is going on.


The request, in this case is done by func fetchJson01(). fetchJson01, makes the call, anticipates a json dictionary of [String:String]. The completion handler, completion, is a closure. In otherwords, one of the arguments made by fetchJson01() is a closure. When “@escaping” is added to the closures input, it means the data will be expected asynchronously.


When something asynchronoly occurs, it occurs on another thread. If that impacts the UI, it needs to be redirected to the main thread by the use of “OperationQueue.main.addOperation{}”. So I stick the completion inside the curly braces of OperationQueue.main.addOperation{}.


  // create dataTask and send request
  func fetchJson01(completion: @escaping([String:String]) -> Void){
      let url = makeUrl()
      let request = URLRequest(url:url)
      let task = session.dataTask(with: request) { (data,response,error) in
          guard let unwrapped_data = data else {
              print("Data is nil")
          do {
              let jsonDict = try JSONSerialization.jsonObject(with: unwrapped_data, options: []) as! [String:String]
              OperationQueue.main.addOperation {
          } catch {
              print("Error converting to json")

Github repo with full code here.

API Endpoint Requesty is making a request to


  @main.route("/json01", methods=["GET","POST"])
  def json01():"-- in json01--")
      return jsonify({"item1":"Something cool!", "item2":"somethign necessary"})  


Returns json dictionary.