Swift 3.0发布请求没有正确发送参数?

时间:2017-07-14 05:53:04

标签: ios swift http http-post

我正在尝试执行此发布请求,但我取得了成功,但用户数据Full Name,Gender和DOB未在服务器上更新,所有都设置为null。我认为我传递的数据不会正确地进入服务器。这是我的代码:

func syncPostRequst() -> Bool {

    let plist = PlistHelper()
    var tokenString = plist.getAnyValueFromPlistFile("ACCESS_TOKEN")

    //create the url with URL
    let url = URL(string: "http://192.168.1.3:8080/api/User/UpdateBasicUserDetails")! //change the url

    //create the session object
    let session = URLSession.shared

    let json: [String: Any] = ["DateOfBirth": "2010.3.3", "FullName": "Anushka Edited iOS", "Gender":"Male"]

    let jsonData = try? JSONSerialization.data(withJSONObject: json)
    NSLog("JDATASE: \(jsonData)")
    NSLog("JDATASEQQQ: \(JSON(jsonData))")

    //now create the URLRequest object using the url object
    var request = URLRequest(url: url)
    request.httpMethod = "POST" //set http method as POST

    // insert json data to the request
    request.httpBody = jsonData
    request.addValue("Bearer \(tokenString)", forHTTPHeaderField: "Authorization")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    //create dataTask using the session object to send data to the server
    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
        print("json222:::\(JSON(data!))")
        NSLog("request9900: \(request)")
        guard error == nil else {
            return
        }

        guard let data = data else {
            return
        }

        do {
            //create json object from data
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print("json:::\(json)")
                // handle json...
            }

        } catch let error {
            print("ERRROR Caught: \(error.localizedDescription)")
        }
    })
    task.resume()

    return true
}

我正在使用Swift 3.0,有谁知道这个原因?

0 个答案:

没有答案