“Permintaan URL Swift” Kode Jawaban

Permintaan URL Swift

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()
Adventurous Anteater

iOS membuat permintaan http

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()
}
Gleaming Gazelle

Jawaban yang mirip dengan “Permintaan URL Swift”

Pertanyaan yang mirip dengan “Permintaan URL Swift”

Lebih banyak jawaban terkait untuk “Permintaan URL Swift” di Swift

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya