【问题标题】:TRON swift response headerTRON 快速响应标头
【发布时间】:2017-05-11 07:55:38
【问题描述】:

您好,我正在将 TRON 框架用于 iOS swift 项目,但在查找正确的文档以访问我的请求的响应标头时遇到了一些麻烦。

对于后端,我使用的是 Wordpress API v2,我想要获得的是 X-WP-Total 和 X-WP-TotalPages 标头。

这是我的请求函数:

let tron = TRON(baseURL: "http://example.com/wp-json/wp/v2")

func getFeed(){

let request: APIRequest<JSONFeed,JSONError> = tron.request("/posts")

request.perform(withSuccess: {(response) in
    print("Ready to parse")
    self.articlesFeatured = (response.articles! as NSArray) as? [listingObject]

    DispatchQueue.main.async {
        self.collectionListing.reloadData()
    }

    }, failure: {(err) in
        print("Error ", err)
    })
}

这些是用于请求的方法:

class JSONFeed: JSONDecodable{
    var articles: [listingObject]? = []
    required init(json: JSON) throws{
        let array = json.array

        for articleJson in array! {
            let article = listingObject()

            let title = articleJson["title"].stringValue

            article.title = title                
            self.articles?.append(article)
        }
    }
}

class JSONError: JSONDecodable{
    required init(json:JSON) throws{
        print("Got an Error")
    }
}

请求和方法解析工作正常,但我只得到响应值,但没有得到标头、状态码等附加信息。

【问题讨论】:

    标签: ios json swift httprequest


    【解决方案1】:

    您可以在完成闭包中使用包含 Alamofire.Response 的 performCollectingTimeline(withCompletion:) 方法:

    request.performCollectingTimeline(withCompletion: { response in
        print(response.timeline)
        print(response.result)
        print(response.response)
    })
    

    【讨论】:

    • 非常感谢您的工作!,在检查该方法的文档后,我发现您可以像这样链接 AlamoFire.Request 方法:?.response(completionHandler: {response in print(response .response?.allHeaderFields) })
    【解决方案2】:

    使用 suhit-patil 的响应,我能够通过以下操作找到解决方案:

        request.performCollectingTimeline { (response) in
            response.response?.allHeaderFields.forEach({ (key, value) in
                print("key: \(key) - Value: \(value)")
            })
        }
    

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 2016-08-07
      • 2018-02-25
      • 2018-05-02
      • 1970-01-01
      • 2018-08-19
      • 2013-02-20
      • 1970-01-01
      相关资源
      最近更新 更多