0

I'm trying to make a request (GET) to a REST API. This API returns airport weather reports in JSON format.

What I am trying to achieve is the following: The user inputs the identification code of an airport, which gets amended to the url. When he/she presses the 'Request' button, a GET request is made to the API which will then show the result in a UILabel.

I've come so far as being able to make the call using a button, and the response from the server is being printed in the console, but I can't get it printed in the UILabel.

This is my code so far:

//
//  ViewController.swift
//  urltest
//
//  Created by Stefan Oomen on 06/05/2020.
//  Copyright © 2020 FlyTechSoft. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var metarResponse: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    @IBAction func onButtonClick(_ sender: UIButton) {

        let url = URL(string: "hhttps://website.rest/api/metar/location?options=&airport=true&reporting=true&format=json&onfail=cache")!
        var request = URLRequest(url: url)
        request.addValue("My_API_KEY", forHTTPHeaderField: "Authorization")

        let task = URLSession.shared.dataTask(with: request) { data, response, error in
          if let response = response {
            print(response)

            if let data = data, let body = String(data: data, encoding: .utf8) {
              print(body)
            }
          } else {
            print(error ?? "Unknown error")
          }
        }

        task.resume()
    }

}

In the GET URL I only need to change the 'Location?' portion, using the input from the user from the 'icaoTextField' UITextField:

https://website.rest/api/metar/location?options=&airport=true&reporting=true&format=json&onfail=cache

I'm quite new to this, so all help is very welcome. Thank you! PS. Small subquestion: is it possible to select and use only specific portions of a REST API call's result?

El_Stevo
  • 31
  • 5

1 Answers1

0

The function metarResponse which is setting the UILabel's text value never gets called anywhere.

Try replacing -

func metarResponse() {
  self.metarresponse.text = response as? String
}

with -

self.metarresponse.text = response ?? "Something went wrong"

is it possible to select and use only specific portions of a REST API call's result?

Certainly. You need to parse the result and extract the desired key value pairs. API responses are usually in the JSON fromat and you could pretty much extract any value out of it, if you know how to loop through and access elements of an array/dictionary.

Animesh K
  • 78
  • 6
  • Hi Animesh, I had a deep look at my code again, and finally got around to programming a button I created using storyboard to do the API GET call when tapped. Now I need to take a few portions from the response (as the whole JSON response is quite large) and simply those in a number of different UILabels. I've edited my question at the top with my new code. My apologies for asking dumb questions, just a enthousiast trying to learn Swift :) – El_Stevo May 06 '20 at 15:09