Answers for "swift url request"

0

swift url request

import Foundation

let apiUrl = URL(string: "https://api.chucknorris.io/jokes/random")!
let dataTask = URLSession.shared.dataTask(with: apiUrl) { (data, response, error) in
    guard let data = data, error == nil else {
        print("no data")
    }
    let jsonResult = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
    print(jsonResult["value"])
}
dataTask.resume()
Posted by: Guest on May-22-2021
0

ios make http request

let Url = String(format: "http://10.10.10.53:8080/sahambl/rest/sahamblsrv/userlogin")
    guard let serviceUrl = URL(string: Url) else { return }
    let parameters: [String: Any] = [
        "request": [
                "xusercode" : "YOUR USERCODE HERE",
                "xpassword": "YOUR PASSWORD HERE"
        ]
    ]
    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "POST"
    request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
    guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
        return
    }
    request.httpBody = httpBody
    request.timeoutInterval = 20
    let session = URLSession.shared
    session.dataTask(with: request) { (data, response, error) in
        if let response = response {
            print(response)
        }
        if let data = data {
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                print(json)
            } catch {
                print(error)
            }
        }
    }.resume()
}
Posted by: Guest on November-12-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language