Swift 带有 url 个参数的 GET 请求

Swift GET request with url parameters

我正在构建一项调查,当用户回答问题时,我将答案附加到带有参数的 url 字符串中,然后向我的服务器发送获取请求。因此,对于每个答案,都有一个由所选答案、时间戳和调查的唯一 ID 组成的记录。

我不确定执行此操作的最佳方法,但这是我目前所采用的方法。

我创建 url 并查询项目。

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()
}

最后我在按下回答时调用发送请求。

@IBAction func answerPressed(_ sender: UIButton) {

    if sender.tag == selectedAnswer {
        questionNumbers += 1
    }
    storedAnswer = [sender.tag]
//        storedAnswer.append(sender.tag)
    print(storedAnswer)

    sendRequest("\(urlComponents)", parameters: ["": ""]) { responseObject, error in
        guard let responseObject = responseObject, error == nil else {
            print(error ?? "Unknown error")
            return
        }


        // use `responseObject` here
    }

    questionNumbers += 1
    updateQuestion()
}

现在,当我 运行 返回带有查询项的字符串时,但是当我 运行 发送请求时,我收到未知错误。我觉得好像我做错了什么。对于 "use responseObject here" 区域,我在其中放了什么。我有点困惑。另外,当我调用发送请求时,我应该在参数值中输入什么。现在它们只是参数:["": ""]。我觉得好像我很亲近。非常感谢任何帮助。

首先,您所做的工作比可能需要的多得多。您将查询字符串参数编码为 URLComponents 是正确的。然后,在您的发送中,您正在分解 URL 并解析组件,然后重新编码它们。您还进行了很多力展开,这很脆弱并且隐藏了问题。

这是在适合我的 playground 中简化的代码:

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))")
    }
}

当我 运行 针对服务器 URL returns 有效 JSON 时,我得到:

Got an answer: Optional(["image": {
    href = "https://example.com";
}, "object_types": {
    card =     {
        fields =         {
        };
        pollable = 1;
    };
}])