0

I am pretty much a beginner in Swift and I am trying to pass a value to make a call to an API with the value being from an input field. The API is given below. How do I write code to pass value for ZIP_CODE from a text field?

https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/ZIP_CODE/JSON

@IBOutlet var zipLabel: UILabel!
@IBOutlet var zipInput: UITextField!
@IBOutlet var jsondataLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    getJSON()      
}

@IBAction func btnAction() {
    zipLabel.text = zipInput.text
}

func getJSON(){
    let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/10001/JSON")
    let request = URLRequest(url: url as! URL )
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    let task = session.dataTask(with: request){(data, response, error ) -> Void in
        if error == nil{
            DispatchQueue.main.async(execute: {
                let jsondata = JSON(data : data!)

                print(jsondata)
                print("-----")
                print(jsondata[0]["UV_INDEX"])

                let result =  jsondata[0]["UV_INDEX"].stringValue

                self.jsondataLabel.text = result
            })
        }else{
            print("There was an error")    
        }
    }
    task.resume()
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vignesh
  • 131
  • 2
  • 5
  • 13
  • you can go through this link .http://stackoverflow.com/questions/31937686/how-to-make-http-post-request-with-json-body-in-swift – Sreejith S Apr 03 '17 at 04:43
  • I have added my code. I get the data but it is static since I have defined the Zip Code Manually, I want the data from text field. Could you please help me with that . – Vignesh Apr 03 '17 at 04:59
  • try this http://stackoverflow.com/questions/36910463/send-post-variables-to-my-api/36910697#36910697 – Jayesh Miruliya Apr 03 '17 at 05:08
  • Hey Jayesh, thanks but I don't find the source of the post passing any value from text field to the API url – Vignesh Apr 03 '17 at 05:51

2 Answers2

1
func getJSON(_ zipcode : Int){



            let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/\(zipcode)/JSON")

            let request = URLRequest(url: url as! URL )

            let config = URLSessionConfiguration.default

            let session = URLSession(configuration: config)



            let task = session.dataTask(with: request){(data, response, error ) -> Void in

                if error == nil{

                    DispatchQueue.main.async(execute: {

                        let jsondata = JSON(data : data!)

                        print(jsondata)

                        print("-----")

                        print(jsondata[0]["UV_INDEX"])

                        let result =  jsondata[0]["UV_INDEX"].stringValue

                        self.jsondataLabel.text = result
                    })

                }else{

                    print("There was an error")    
                }

            }
            task.resume()
    }
anshul king
  • 558
  • 4
  • 17
0

If your API gives you the response on GET Request you can use the following code -

@IBOutlet var zipLabel: UILabel!
@IBOutlet var zipInput: UITextField!
@IBOutlet var jsondataLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func btnAction() {
    zipLabel.text = zipInput.text
    getJSON(zipcode: zipInput.text)
}

func getJSON(zipcode: String){
    let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/10001/JSON&zipcode="+zipcode)
    let request = URLRequest(url: url as! URL )
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    let task = session.dataTask(with: request){(data, response, error ) -> Void in
        if error == nil{
            DispatchQueue.main.async(execute: {
                let jsondata = JSON(data : data!)

                print(jsondata)
                print("-----")
                print(jsondata[0]["UV_INDEX"])

                let result =  jsondata[0]["UV_INDEX"].stringValue

                self.jsondataLabel.text = result
            })
        }else{
            print("There was an error")    
        }
    }
    task.resume()
}

But Please note that this code won't work if your API gives you the response on POST request.

Abhishek
  • 3,304
  • 4
  • 30
  • 44