How to Read Swift Headers

How to read swift headers

Let's break it down:

public func split(maxSplit: Int = default, allowEmptySlices: Bool = default, @noescape isSeparator: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.SubSequence]

maxSplit: The first parameter, maxSplit, allows you to specify the maximum number of pieces that the sequence will be split into. The default is Int.max.

allowEmptySlices: The second parameter, allowEmptySlices specifies whether or not two consecutive separators in the sequence should lead to an empty slice. The default is false. For example if you had a String, "A..B", and you split on the . character, you could end up with either two (["A", "B"]) or three (["A", "", "B"]) items in the output array depending on what you pass for this parameter.

isSeparator: The last parameter is the closure that you pass to identify where to split the sequence.

Since both maxSplit and allowEmptySlices have default arguments, you don't need to include them in your function call unless you want to change them. The only parameter that you have to supply is the isSeparator closure.

In your case, you called:

someString.characters.split { $0 == "."}

...which is the equivalent of:

someString.characters.split(maxSplit: Int.max, allowEmptySlices: false) { $0 == ".' }

You could also write your function call like this:

someString.characters.split(isSeparator: { $0 == "." })

The way that you have written it makes use of the "trailing closure" syntax. If a function takes a closure as it's final argument, you can move the closure outside the parentheses like this:

someString.characters.split() { $0 == "." }

And if the function takes only one argument (not counting any default arguments that you are not supplying) then you can omit the parentheses altogether:

someString.characters.split { $0 == "." }

At the highest level, what happens is that split iterates through the sequence of characters. It tests each character using the supplied closure, and if the closure returns true, it splits the sequence on that character. In your case it will split the sequence of characters every time it locates a ".".

Some other notes:

rethrows: The whole function is marked rethrows. It will throw an error, but only if the closure that you pass for the isSeparator argument throws an error itself. Note that the isSeparator parameter allows you to pass a closure that throws an error, but you don't have to. Any time a function accepts a closure that throws an error, it will also accept a closure that does not throw. This is because non-throwing functions are a sub-type of throwing functions.

@noescape: The isSeparator parameter is marked @noescape. That simply means that nothing in the closure will survive past the end of the call to split.

How parse HTTPHeader in Swift

Try with this method.

 func getAuthenticationKey(headerInfo: [(String, String)]) -> String?{
for item in headerInfo.enumerated(){
print("key: \(item.element.0), values : \(item.element.1)")

let headerKeys = item.element.1.split(separator: ",")

for item in headerKeys {
if item.contains("nonce") {
let authinticationKey = item.split(separator: "=")[1]
return String(authinticationKey)
}
}

}
return nil
}

USE:

 let headerInfo = [("Connection", "close"), ("Date", "Sun, 22 Dec 2019 07:36:50 GMT"), ("WWW-Authenticate", "Digest realm=\"Testserver\", domain=\"/\", nonce=\"KZN/eQFuVy7iONz+Mts27+nj2GwVMVSz\", algorithm=MD5, qop=\"auth\", stale=false"), ("Cache-Control", "must-revalidate,no-cache,no-store"), ("Content-Type", "text/plain;charset=utf-8"), ("Content-Length", "16"), ("Server", "Jetty(9.4.19.v20190610)")]
print(getAuthenticationKey(headerInfo: headerInfo))

Output:

"KZN/eQFuVy7iONz+Mts27+nj2GwVMVSz"

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

Read email headers with flags - Mailcore & swift

So after banging my head the only solution which worked for me is append flags by taking its union.

            let kind = MCOIMAPMessagesRequestKind()
let headers = kind.union(MCOIMAPMessagesRequestKind.headers)
let request = headers.union(MCOIMAPMessagesRequestKind.flags)

I know it's not best but it worked for me.

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


Related Topics



Leave a reply



Submit