📜  Swift REST Api 调用 - Shell-Bash 代码示例

📅  最后修改于: 2022-03-11 14:51:19.189000             🧑  作者: Mango

代码示例1
let params = ["username":"john", "password":"123456"] as Dictionary

var request = URLRequest(url: URL(string: "http://localhost:8080/api/1/login")!)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
    print(response!)
    do {
        let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary
        print(json)
    } catch {
        print("error")
    }
})

task.resume()