fotsumi
fotsumi

Reputation: 15

TRON swift response header

Hi i'm using TRON framework for an iOS swift project and i'm having some trouble finding the correct documentation to access the response header for my request.

For the backend i'm using Wordpress API v2 and what i'm trying to get is the X-WP-Total and X-WP-TotalPages headers.

This is my request function:

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

And these are the methods used for the request:

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

The request and method parsing works properly but i only get the response value but no the additional information like header, status code, etc.

Upvotes: 0

Views: 551

Answers (2)

Jorge Casariego
Jorge Casariego

Reputation: 22212

Using the response of suhit-patil I was able to reach the solution by doing the following:

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

Upvotes: 0

Suhit Patil
Suhit Patil

Reputation: 12023

you can use performCollectingTimeline(withCompletion:) method that contains Alamofire.Response inside completion closure:

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

Upvotes: 1

Related Questions