嗨,我正在使用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")
    }
}

请求和方法解析工作正常,但我只得到响应值,而没有额外的信息,如头、状态码等。

最佳答案

您可以使用包含Alamofire的performCollectingTimeline(withCompletion:)方法。完成闭包内的响应:

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

10-06 11:25