带有url参数的Swift GET请求
我正在构建一个调查,当用户回答一个问题时,我会将答案附加到带有参数的url字符串中,并向我的服务器发送get请求。因此,对于每个答案,都有一个由所选答案、时间戳和调查的唯一id组成的记录 我不确定做这件事的最佳方式,但这就是我目前所拥有的 我创建url和查询项带有url参数的Swift GET请求,swift,swift4,get-request,nsurlcomponents,Swift,Swift4,Get Request,Nsurlcomponents,我正在构建一个调查,当用户回答一个问题时,我会将答案附加到带有参数的url字符串中,并向我的服务器发送get请求。因此,对于每个答案,都有一个由所选答案、时间戳和调查的唯一id组成的记录 我不确定做这件事的最佳方式,但这就是我目前所拥有的 我创建url和查询项 var urlComponents: URLComponents { let resultID = surveyQuestions.resultId print("\(String(describing: resultI
var urlComponents: URLComponents {
let resultID = surveyQuestions.resultId
print("\(String(describing: resultID))")
let resultResponseID = surveyQuestions.questions[surveyResultResponseId]
print("\(String(describing: resultResponseID))")
let questionIndex = questionNumbers
print("\(String(describing: questionIndex))")
var urlComponents = URLComponents(string: "My String URL")
urlComponents?.queryItems = [
URLQueryItem(name: "surveyResultsId", value: "\(String(describing: resultID))"),
URLQueryItem(name: "surveyResultsResponseId", value: "\(String(describing: resultResponseID))"),
URLQueryItem(name: "questions", value: "\(questionIndex)"),
URLQueryItem(name: "selectedAnswer", value: "\(storedAnswer)")
]
let url = urlComponents?.url
print(url!.absoluteString as Any)
return urlComponents!
}
然后我构建发送请求
func sendRequest(_ url: String, parameters: [String: String], completion: @escaping ([String: Any]?, Error?) -> Void) {
var components = URLComponents(string: url)!
components.queryItems = parameters.map { (key, value) in
URLQueryItem(name: key, value: value)
}
components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
let request = URLRequest(url: components.url!)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, // is there data
let response = response as? HTTPURLResponse, // is there HTTP response
(200 ..< 300) ~= response.statusCode, // is statusCode 2XX
error == nil else { // was there no error, otherwise ...
completion(nil, error)
return
}
let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
completion(responseObject, nil)
print("This is the \(responseObject!)")
}
task.resume()
}
现在,当我运行此命令时,我会返回带有查询项的字符串,但当我运行send请求时,我会得到未知错误。我觉得我好像做错了什么事。对于“use responseObject here”区域,我应该在其中添加什么内容。我有点困惑。另外,当我调用send请求时,我应该在参数值中输入什么。现在,它们只是参数:[“”:“”]。我觉得我好像很亲近。非常感谢您的帮助。首先,您所做的工作比可能需要的多得多。您正在将查询字符串参数编码为
URLComponents
,这是正确的。然后,在发送中,您将分解URL并解析出组件,然后对其重新编码。你也在做大量的强制展开,这是脆弱的,隐藏了问题
下面是你在操场上简化的代码,对我来说很有用:
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let sampleURL = "https://someserver.com/somepath"
func sendRequest(_ url: URL, completion: @escaping ([String: Any]?, Error?) -> Void) {
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, // is there data
let response = response as? HTTPURLResponse, // is there HTTP response
(200 ..< 300) ~= response.statusCode, // is statusCode 2XX
error == nil else { // was there no error, otherwise ...
completion(nil, error)
return
}
let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
completion(responseObject, nil)
}
task.resume()
}
var urlComponents: URLComponents? {
let resultID = "resultID123"
let resultResponseID = "responseID456"
let questionIndex = "questionNumbers1"
var urlComponents = URLComponents(string: sampleURL)
urlComponents?.queryItems = [
URLQueryItem(name: "surveyResultsId", value: "\(String(describing: resultID))"),
URLQueryItem(name: "surveyResultsResponseId", value: "\(String(describing: resultResponseID))"),
URLQueryItem(name: "questions", value: "\(questionIndex)"),
URLQueryItem(name: "selectedAnswer", value: "\("storedAnswer1")")
]
return urlComponents
}
if let urlComponents = urlComponents, let url = urlComponents.url?.absoluteURL {
sendRequest(url) { (result, error) in
print("Got an answer: \(String(describing: result))")
}
}
谢谢你的快速回复。对于if let urlcomponents=urlcomponents,我发现这个错误“let”不能嵌套在另一个“var”或“let”模式中。不知道为什么。当我将if let urlComponents=urlComponents,let url=urlComponents.url?.absoluteURL{sendRequest(url){(result,error)打印(“获得答案:(String(description:result))”)}放在我的answerPressed按钮中时,它工作正常。我还注意到发送请求时得到的响应不包括url和urlqueryitems。它只返回一个可选响应,不包含urlcomponents.URLQueryItems只是一个用于将参数编码到查询字符串中的构造。当您在URL组件上调用“absoluteURL”时,它会将所有组件编码到URL本身(例如)。如果需要,您可以随时恢复它们,但它们都在那里。有没有办法删除它们?在queryitems之前?我尝试在sampleUrl上使用replacingOccurrences(of:“?”,with:”),但这不起作用。您是否错过了
~=
重载的代码,或者它本身可以工作?
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let sampleURL = "https://someserver.com/somepath"
func sendRequest(_ url: URL, completion: @escaping ([String: Any]?, Error?) -> Void) {
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, // is there data
let response = response as? HTTPURLResponse, // is there HTTP response
(200 ..< 300) ~= response.statusCode, // is statusCode 2XX
error == nil else { // was there no error, otherwise ...
completion(nil, error)
return
}
let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
completion(responseObject, nil)
}
task.resume()
}
var urlComponents: URLComponents? {
let resultID = "resultID123"
let resultResponseID = "responseID456"
let questionIndex = "questionNumbers1"
var urlComponents = URLComponents(string: sampleURL)
urlComponents?.queryItems = [
URLQueryItem(name: "surveyResultsId", value: "\(String(describing: resultID))"),
URLQueryItem(name: "surveyResultsResponseId", value: "\(String(describing: resultResponseID))"),
URLQueryItem(name: "questions", value: "\(questionIndex)"),
URLQueryItem(name: "selectedAnswer", value: "\("storedAnswer1")")
]
return urlComponents
}
if let urlComponents = urlComponents, let url = urlComponents.url?.absoluteURL {
sendRequest(url) { (result, error) in
print("Got an answer: \(String(describing: result))")
}
}
Got an answer: Optional(["image": {
href = "https://example.com";
}, "object_types": {
card = {
fields = {
};
pollable = 1;
};
}])