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
headersA dictionary of headers to be added to the requests.
urlMatchingAn instance conforming to
URLMatchingthat 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
sessionThe session that will contain the task after creation.
taskThe task that is about to be created.
requestThe request with which the task will be created.
completionHandlerA completion handler that must be called after modifying the request.