带有String和NSDate Swift的HTTP POST请求

时间:2017-02-08 15:12:21

标签: swift httprequest

我在有效负载上有这个参数来向后端发送POST请求

 '{"token":"xxxx", "extra_information": {"expires_in": xxxx, "refresh_token": "xxx", "user_id": "user_uuid", "token_type": "Bearer"}}'

参数" xxxx"将通过整合来实现。我尝试创建一个发送此功能的函数

    func sendAuth() {
    if let url = NSURL(string: "https://xxxxxxxxxxxx"){
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST" 
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        let token = AccessToken?()
        let params = ["token" : (token?.tokenString)!, "refresh_token" : (token?.refreshToken)!,"expires_in" : (token?.expirationDate)!, "user_id" : "uber_uuid"  , "token_type" : "Bearer"] as Dictionary <String,String>

        let httpData = NSKeyedArchiver.archivedDataWithRootObject(params)
        request.HTTPBody = httpData
        let session = ServicesUtils.BaseSessionManager()
        session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
            var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
            print("\(strData)")
        }).resume()

写完这个xCode后显示错误&#34;无法将Type NSDate的值转换为预期的字典值Type String&#34;由于参数expires_in收到NSDate。

编辑1 - 更改后Dictionay收到strData发生错误&#34;无法将类型&#39; NSURLResponse的值转换为预期的参数类型&#39; NSData&#39;&#34;

1 个答案:

答案 0 :(得分:0)

因为您将params投射为Dictionary <String,String>,这就是为什么它不会在您的字典中接受NSDate作为值。

您需要像Dictionary <String,AnyObject>一样更改它,它会起作用。