gpt4 book ai didi

ios - 如何在 Swift 3 和 URLRequest 上使用 httpMethod = POST?

转载 作者:行者123 更新时间:2023-11-28 11:00:40 24 4
gpt4 key购买 nike

我正在使用 json 使用 GET 方法获取代码,但是当我通过添加将其转换为 POST 时;

  request.httpMethod = "POST"

给我;

Cannot assign to property: 'httpMethod' is a get-only property Error

我下面的代码如何修复它?我如何将其转换为 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 方法调用代码 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 结构:

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/form-data 还是 json 格式,这完全取决于。但总的来说,假设您正在使用我提到的 URLRequest 结构,您应该使用序列化程序将其转换为 Data 并像这样使用它:

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

通常,您的 API 需要将适当的 Content-Type header 添加到您的 URLRequest:

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

关于ios - 如何在 Swift 3 和 URLRequest 上使用 httpMethod = POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40740575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com