使用application / x-www-form-urlencoded的Swift3 POST请求

时间:2019-03-03 14:18:20

标签: swift post nsurlsession

我正在向我的API发出发布请求。这是一个登录请求,似乎出了点问题, 这是我编写的代码:

let myUrl = URL(string: "url")

let postString = ["Username": "username",
                  "Password": "pass",
"grant_type": "password"] as [String: String]

var request : URLRequest = URLRequest(url: myUrl!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
do {
    request.httpBody = try JSONSerialization.data(withJSONObject: postString, options: .prettyPrinted)

} catch let error {
    print(error.localizedDescription)
    displayMessage(userMessage: "Something went wrong.Try again. A3")
    return
}

let task = URLSession.shared.dataTask(with: request)
{}
task.resume()

似乎API请求中出现了问题

0 个答案:

没有答案