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?
ok approach you're answer with this solution
Related Topics
Dispatchqueue.Main.Asyncafter Is Inaccurate
How to Convert Int to Byte Array of 4 Bytes in Swift
Given a Hexadecimal String in Swift, Convert to Hex Value
Swift - Class Method Which Must Be Overridden by Subclass
How Do Closures Capture Values from Previous Calls
Able to Write/Read File But Unable to Delete File Swift
Why Can You Assign Non-Optional Values to Optional Types in Swift
Peak Detection for Growing Time Series Using Swift
Nsnumberformatter:Show 'K' Instead of ',000' in Large Numbers
Detail View Is Not Updated When the Model Is Updated (Using List) Swiftui
Bounce Rays with Enumeratebodies Alongraystart
How to Properly Implement the Equatable Protocol in a Class Hierarchy
How to Make Protocol Associated Type Require Protocol Inheritance and Not Protocol Adoption
Which Swift Character Count Should I Use When Interacting with Nsstring APIs
In Swift, How to Remove a Uiview from Memory Completely
Reusing Security Scoped Bookmark
How to Combine Two Strings (Date & Time) into a New Date in Swift 3