JSONSerialization.jsonObject

时间:2018-03-01 02:32:19

标签: ios swift nsjsonserialization

我是用JSON Token开发iOS的新手,我收到了一个不支持的_grant_type错误。我正在开发的代码如下

let params = ["grant_type": "password", "username": userEmail, "password": userPassword]

let url = URL(string: "http://test/SPUSR/Token")

var request = URLRequest(url: url!)

request.httpMethod = "POST"

request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

try? request.httpBody = JSONSerialization.data(withJSONObject: params, options: [])

let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)

let task = session.dataTask(with: request) { (data, response, error) in
    if let data = data {
        let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]

        self.token = json!!["access_token"] as? String
    }

    if let error = error {
        print(error.localizedDescription)
    }
}

task.resume()

随着邮递员的一切运作良好。我的API是在技术C#

下开发的

1 个答案:

答案 0 :(得分:0)

消息" unsupported_grant_type"明确告诉您,您的服务器并不期望该请求。您正在尝试POST:" grant_type":"密码",正文中的凭据 - 这意味着内容类型必须是" application / x- WWW的形式urlencoded的&#34 ;.请参阅Postman中的控制台。