CustomHeadersObserver
public class CustomHeadersObserver : SAPURLSessionObserving
CustomHeadersObserver is responsible for adding custom headers to HTTP requests made through the SAPURLSession
.
To utilize this functionality, you need to register an instance of this observer with an SAPURLSession
.
Here is an example of how to use the CustomHeadersObserver
:
let urlSession: SAPURLSession = <#SAPURLSession instance#>
let headers = ["Authorization": "Bearer token", "X-Custom-Header": "value"]
let urlMatcher = URLMatcher(pattern: "https://api.example.com/")
let customHeadersObserver = CustomHeadersObserver(headers, urlMatcher)
urlSession.register(customHeadersObserver)
-
Initializes a new CustomHeadersObserver instance.
Declaration
Swift
public init(_ headers: [String : String], _ urlMatching: URLMatching = MatchAll())
Parameters
headers
A dictionary of headers to be added to the requests.
urlMatching
An instance conforming to
URLMatching
that determines which URLs will have custom headers added. Defaults toMatchAll
. -
Invoked before a request is sent. The observer can modify the request by adding custom headers.
Declaration
Swift
public func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, willSend request: URLRequest, completionHandler: @escaping (SAPURLSession.RequestDisposition) -> Void)
Parameters
session
The session that will contain the task after creation.
task
The task that is about to be created.
request
The request with which the task will be created.
completionHandler
A completion handler that must be called after modifying the request.