Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift 3上使用httpMethod=POST,URLRequest?_Ios_Json_Swift_Httprequest - Fatal编程技术网

Ios 如何在Swift 3上使用httpMethod=POST,URLRequest?

Ios 如何在Swift 3上使用httpMethod=POST,URLRequest?,ios,json,swift,httprequest,Ios,Json,Swift,Httprequest,我有使用GET方法获取代码的json工作,但是当我通过添加将其转换为POST时 request.httpMethod = "POST" 给我 无法分配给属性:“httpMethod”是仅获取属性错误 下面我的代码怎么能修复它?如何将其转换为POST @discardableResult open func getLoginMethod(_ method: String, parameters: String, completionHandler: @escaping (_ lo

我有使用
GET
方法获取代码的
json
工作,但是当我通过添加将其转换为
POST

  request.httpMethod = "POST"
给我

无法分配给属性:“httpMethod”是仅获取属性错误

下面我的代码怎么能修复它?如何将其转换为
POST

 @discardableResult
    open func  getLoginMethod(_ method: String, parameters: String, completionHandler: @escaping (_ login: [Login]?, _ error: Error?) -> Void) -> URLSessionTask! {
        let session = URLSession.shared
        guard let url = NSURL(string: parameters) else {
            completionHandler(nil, myErrors.InvalidUrlError)
            return nil
        }
        let request = NSURLRequest(url: url as URL)

        let task = session.dataTask(with: request as URLRequest) { data, response, error in
            if error != nil {
                completionHandler(nil, myErrors.getError)
            } else {
                do {
                    let login = try self.getLogin(jsonData: data! as NSData)
                    completionHandler(login, nil)
                } catch {
                    completionHandler(nil, error)
                }
            }
        }
        task.resume()

        return task
    }
还有下面的调用代码,用于
get
method我在
parameters
中的变量,用于
POST
方法,我需要做哪些更改

  AWLoader.show(blurStyle: .dark, shape: .Circle)

        DispatchQueue.global(qos: .background).async {
            myApp.sharedInstance().getLoginMethod("", parameters: "http://bla.com/Post?Phone=\(self.phone.text)") {login, error in
                if error != nil {
                    DispatchQueue.main.async {
                        //  self.showAlert()

                        print(error!)

                        AWLoader.hide()
                    }
                } else {
                    DispatchQueue.main.async {
                        self.getlogin = login!
                        //    self.collectionView.reloadData()
                        //       AWLoader.hide()

                        //  self.loginButton.alpha = 0
                        print(self.getlogin)


                        AWLoader.hide()
                    }
                }
            }


        }

谢谢

您需要使用
NSMutableURLRequest
才能修改
httpMethod
。使用
NSURLRequest
不行

例如:

let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
但更好的是,您应该使用
URLRequest
struct:

var request = URLRequest(url: url)
request.httpMethod = "POST"
您的重构代码还有其他改进:

@discardableResult
open func getLoginMethod(_ method: String, parameters: String,
                     session: URLSession = .shared,
                     completionHandler: @escaping (_ login: [Login]?, _ error: Error?) -> Void) -> URLSessionTask! {

    guard let url = URL(string: parameters) else {
        completionHandler(nil, myErrors.InvalidUrlError)
        return nil
    }
    var request = URLRequest(url: url)
    request.httpMethod = method
    let task = session.dataTask(with: request) { data, _, _ in
        guard let responseData = data else {
            completionHandler(nil, myErrors.getError)
            return
        }
        do {
            let login = try self.getLogin(jsonData: data as NSData)
            completionHandler(login, nil)
        } catch {
            completionHandler(nil, error)
        }
    }
    task.resume()

    return task
}
编辑: 回答问题的第二部分。这取决于您的API所期望的数据格式——它应该是
multipart/formdata
还是
json
格式,这完全取决于您的API。 但一般来说,假设您正在使用我提到的
URLRequest
struct,您应该使用序列化程序将其转换为
Data
,并像这样使用它:

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = <your parameters converted to Data>

您需要使用
NSMutableURLRequest
才能修改
httpMethod
。使用
NSURLRequest
不行

例如:

let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
但更好的是,您应该使用
URLRequest
struct:

var request = URLRequest(url: url)
request.httpMethod = "POST"
您的重构代码还有其他改进:

@discardableResult
open func getLoginMethod(_ method: String, parameters: String,
                     session: URLSession = .shared,
                     completionHandler: @escaping (_ login: [Login]?, _ error: Error?) -> Void) -> URLSessionTask! {

    guard let url = URL(string: parameters) else {
        completionHandler(nil, myErrors.InvalidUrlError)
        return nil
    }
    var request = URLRequest(url: url)
    request.httpMethod = method
    let task = session.dataTask(with: request) { data, _, _ in
        guard let responseData = data else {
            completionHandler(nil, myErrors.getError)
            return
        }
        do {
            let login = try self.getLogin(jsonData: data as NSData)
            completionHandler(login, nil)
        } catch {
            completionHandler(nil, error)
        }
    }
    task.resume()

    return task
}
编辑: 回答问题的第二部分。这取决于您的API所期望的数据格式——它应该是
multipart/formdata
还是
json
格式,这完全取决于您的API。 但一般来说,假设您正在使用我提到的
URLRequest
struct,您应该使用序列化程序将其转换为
Data
,并像这样使用它:

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = <your parameters converted to Data>

但是我怎样才能寄各种各样的?通常我会在参数内发送它们:“)”就像这样,但在帖子上如何发送它们?我更新了我的问题,在帖子上添加了调用方法代码?如何发送变量?我需要更改哪些?这取决于您需要如何发送它们-什么类型的编码,等等,但基本上您需要将它们添加到
request.httpBody
中。你能把它整合到我的代码中吗?我会通过测试批准你的答案。@SwiftDeveloper你只是在你的初始答案中添加了新的、要求更高、范围更广的问题。因此,它不是一个教您什么是数据序列化并为您编写代码的工具,而是帮助您理解一些编程概念或真正的代码相关问题(如果您有)。我扩展了我的答案,以帮助您解决向
POST
请求添加参数的问题,并解释了一个推理。您只需要阅读一些关于请求参数序列化的内容。公平一点,但是我怎么能寄各种各样的呢?通常我会在参数内发送它们:“)”就像这样,但在帖子上如何发送它们?我更新了我的问题,在帖子上添加了调用方法代码?如何发送变量?我需要更改哪些?这取决于您需要如何发送它们-什么类型的编码,等等,但基本上您需要将它们添加到
request.httpBody
中。你能把它整合到我的代码中吗?我会通过测试批准你的答案。@SwiftDeveloper你只是在你的初始答案中添加了新的、要求更高、范围更广的问题。因此,它不是一个教您什么是数据序列化并为您编写代码的工具,而是帮助您理解一些编程概念或真正的代码相关问题(如果您有)。我扩展了我的答案,以帮助您解决向
POST
请求添加参数的问题,并解释了一个推理。您只需要阅读一些关于请求参数序列化的内容。公平点。
NSURLRequest
是不可变的,
NSMutableURLRequest
是可变的。为什么不使用
URLRequest
(更像Swift)?
NSURLRequest
是不可变的,
NSMutableURLRequest
是可变的。为什么不使用
URLRequest
(更像Swift)?