swift - swift 中的 POST 请求错误

标签 swift nsurlsession

我想成为一种类型的变量来发送到字典服务器但是但是在线上我遇到了问题 let task = session.dataTaskWithRequest(todosUrlRequest) error : Cannot将“NSURL”类型的值转换为预期的参数类型“NSURLRequest”

我有两个问题

1) 这是什么错误?

2)有没有我用过的POST程序是对的?不需要其他任何东西。 ?? 谢谢你的帮助

func data_request (){

            let url = "http://sample.com/api/Flight/GetTicketInformation"
            guard let todosUrlRequest = NSURL(string: url) else {
                print("Error: cannot create URL")
                return
            }

            let request = NSMutableURLRequest(URL: todosUrlRequest)
            request.HTTPMethod = "POST"
            request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

            let newTodo = ["Roundtrip": roundTrip,
                           "OneWay": oneWay,
                           "MultiWay": multiWay,
                           "Adult": numberAdults,
                           "Child": numberchild,
                           "Baby": numberinfant,
                           "SourceCityId": cityIDOrigin,
                           "DestinationCityId": cityIDPurpose,
                           "DepartingDate": raftDate,
                           "ReturningDate": bargashtDate ]
            let jsonTodo: NSData
            do {
                jsonTodo = try NSJSONSerialization.dataWithJSONObject(newTodo, options: [])
                request.HTTPBody = jsonTodo

            } catch {
                print("Error: cannot create JSON from todo")
                return
            }
            request.HTTPBody = jsonTodo

            let config = NSURLSessionConfiguration.defaultSessionConfiguration()
            let session = NSURLSession(configuration: config)

            let task = session.dataTaskWithRequest(todosUrlRequest) {
                (data, response, error) in

                guard let responseData = data else {
                    print("Error: did not receive data")
                    return
                }
                guard error == nil else {
                    print("error calling POST on /todos/1")
                    print(error)
                    return
                }

                // parse the result as JSON, since that's what the API provides
                do {
                    guard let receivedTodo = try NSJSONSerialization.JSONObjectWithData(responseData,
                                                                                        options: []) as? [String: AnyObject] else {
                                                                                            print("Could not get JSON from responseData as dictionary")
                                                                                            return
                    }
                    print("The todo is: " + receivedTodo.description)


                } catch  {
                    print("error parsing response from POST on /todos")
                    return
                }
            }
            task.resume()
        }

最佳答案

let task = session.dataTaskWithRequest(todosUrlRequest)

行上用

request 代替 todosUrlRequest

第二个问题,不知道。对不起

关于swift - swift 中的 POST 请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36643936/

相关文章:

swift - 显示 uitableview 后更改 UITableViewCell 高度

swift - 闭包主体这么长,如何让它变短

iOS 下载前获取文件大小

ios - 模拟 API 测试的 URLSession 是将无法识别的选择器发送到实例

objective-c - NSURLSessionDataTask 运行缓慢

Swift:阻塞 mainThread 直到函数用 completionHandler 完成加载数据/NSURLSession

swift - 将非转义闭包转换为转义闭包

ios - 私有(private)快速协议(protocol)是否可能?

swift - SwiftUI 中使用 Text() 换行

ios - 防止类在特定 iOS 版本上编译