首页 > 解决方案 > Swift 合并 HTTP 请求

问题描述

试图弄清楚如何使用组合进行快速 http 请求。我一直在看 Apple 的这个文档。虽然我没有取得任何进展,而且看起来很简单,所以我不知道我做错了什么。

我习惯于 JavaScript 并使用Fetch API
也习惯于在 Golang 中使用http pkg
但由于某种原因,在 Swift 中这样做真的让我感到困惑,就像它不像我提到的两个那样精简

我将 SwiftUI 与 MVVM 架构一起使用,因此我的目标是在我的视图模型中实现一种与 Golang 服务器通信的方式。

这主要POST需要带有标头和Accept正文的请求AuthorizationJSON

我试过这段代码:

let url = URL(string: "https://api.example.com/user/login")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue(String(format: "Bearer %s", idToken!), forHTTPHeaderField: "Authorization")
        
_ = URLSession.shared.dataTaskPublisher(for: request)
    .tryMap() { element -> Data in
        // not worried about the status code just trying to get any response.
        guard let httpResponse = element.response as? HTTPURLResponse, httpResponse.statusCode >= 200
        else {
            throw URLError(.badServerResponse)
        }
        return element.data
    }
    // .decode(type: LoginResponseData.self, decoder: JSONDecoder()) commenting this out because I just want to see any data.
    .sink(
        receiveCompletion: { print ("Received completion: \($0).") },
        receiveValue: { data in print ("Received user: \(data).")}
    )

并得到这个控制台消息:我在这里nw_protocol_get_quic_image_block_invoke dlopen libquic failed
读到的可能与我的问题无关,但是他们在谈论我认为的 firebase。

我想知道我的代码是否有问题?
或者带有一些JSONAPI 的工作示例,甚至是GET某个网站的 http 请求都可以。

谢谢!

标签: iosswifthttpcombine

解决方案


让我们对您的代码进行非常基本的简化:

    let request = URLRequest(url: URL(string:"https://www.apple.com")!)
    _ = URLSession.shared.dataTaskPublisher(for: request)
        .sink(receiveCompletion: {_ in print("completion")},
              receiveValue: { print($0) })

结果:什么都没有打印


好的,现在让我们按照我在评论中的建议进行操作。我们有一个实例属性:

var storage = Set<AnyCancellable>()

我们将代码更改为如下所示:

    URLSession.shared.dataTaskPublisher(for: request)
        .sink(receiveCompletion: {_ in print("completion")},
              receiveValue: { print($0) })
        .store(in:&self.storage)

我们在控制台中得到了这个(好吧,至少我这样做了):

(data: 74909 bytes, response: <NSHTTPURLResponse: 0x600002e479e0> { URL: https://www.apple.com/ } { Status Code: 200, Headers {
    "Cache-Control" =     (
        "max-age=151"
    );
    "Content-Encoding" =     (
        gzip
    );
    "Content-Length" =     (
        10974
    );
    "Content-Type" =     (
        "text/html; charset=UTF-8"
    );
    Date =     (
        "Fri, 04 Dec 2020 17:45:50 GMT"
    );
    Expires =     (
        "Fri, 04 Dec 2020 17:48:21 GMT"
    );
    Server =     (
        Apache
    );
    "Set-Cookie" =     (
        "geo=US; path=/; domain=.apple.com",
        "ccl=uQisGvuhtgEjEZERqJarcpJqZcmsz2JEaxjveIr8V14=; path=/; domain=.apple.com"
    );
    "Strict-Transport-Security" =     (
        "max-age=31536000; includeSubDomains"
    );
    Vary =     (
        "Accept-Encoding"
    );
    "x-content-type-options" =     (
        nosniff
    );
    "x-frame-options" =     (
        SAMEORIGIN
    );
    "x-xss-protection" =     (
        "1; mode=block"
    );
} })
completion

要理解其中的区别,或许想想赫拉克利特会有所帮助。“你不能两次踏入同一条河流,因为不同的、不同的水源源不断地流淌。” 那条河就是时间

  • 您的代码中,publisher-to-sink 管道被创建并丢弃- 因为它纯粹是此方法的本地,它创建管道并立即结束。与此同时,河流在流淌!因此,在请求甚至有机会开始之前,管道就已经消失了。它永远不会开始,所以永远不会结束。它永远不会完成,所以它永远不会打印。

  • 我的代码中,保留了发布者到接收器的管道(在实例属性中)。就是store这样。因此,请求有机会开始,并且稍后它有机会完成,在管道创建很久之后,而河流一直在流动。


推荐阅读