Get Header Data from a Request Response in Swift

Get header data from a request response in swift

If the response is type of NSHTTPURLResponse you can get header from response.allHeaderFields

As apple documentation says :

A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.

The keys in this dictionary are the header field names, as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.

So to get for example a X-Dem-Auth in response header you can access it in that way :

if let httpResponse = response as? NSHTTPURLResponse {
if let xDemAuth = httpResponse.allHeaderFields["X-Dem-Auth"] as? String {
// use X-Dem-Auth here
}
}

UPDATE

Updated due to comment from Evan R

if let httpResponse = response as? HTTPURLResponse {
if let xDemAuth = httpResponse.allHeaderFields["X-Dem-Auth"] as? String {
// use X-Dem-Auth here
}
}

How to get response headers when using Alamofire in Swift?

As response is of NSHTTPURLResponse type, you should be able to get the headers as followed:

response.allHeaderFields

Swift - Is it possible to decode the HTTP response headers, for request limiting?

These are already decoded for you, into a [AnyHashable: Any] dictionary. To fetch this particular one, you'd check it with something along these lines:

if let remaining = theResponse.allHeaderFields["X-RateLimit-requests-Remaining"] 
as? Int { ... }

How get value of headers in request (URLSession.shared.dataTask(with: request) { (data, response, error)


"sessid=EZEaqDixCNZKjY4HwEXSjuR8lKhqkHXGLX6SoZSmtdUegKP7AkUuFOpUIHygf3M5;".replacingOccurrences(of: "sessid=", with: "").dropLast()
// Result: EZEaqDixCNZKjY4HwEXSjuR8lKhqkHXGLX6SoZSmtdUegKP7AkUuFOpUIHygf3M5

First we remove "sessid=" and then dropping the semicolon.

Swift get headers from UIWebView response

Worked for Swift 3, Swift 4 and Swift 5

func webViewDidFinishLoad(_ webView: UIWebView) { 
let headers = webView.request?.allHTTPHeaderFields
for (key,value) in headers! {
print("key \(key) value \(value)")
}
}

How to retrieve Alamofire response header for a request

If the response is type of NSHTTPURLResponse you can get header from response.allHeaderFields.

So when you use Alamofire responseJSON you can access to NSHTTPURLResponse property like this :

Alamofire.request(.GET, requestUrl, parameters:parameters, headers: headers).responseJSON {
response in
print(response.response?.allHeaderFields)
}

As apple documentation says :

A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.

The keys in this dictionary are the header field names, as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.

So to get for example a content-type in response header you can access it in that way :

if let contentType = response.response?.allHeaderFields["Content-Type"] as? String {
// use contentType here
}

How to pass data into header while using URLSession in Swift?

Sample Image

ok approach you're answer with this solution

Sample Image



Related Topics



Leave a reply



Submit