URLSessionHttpRequest
public class URLSessionHttpRequest : HttpRequest, @unchecked Sendable
URLSession-based implementation of HttpRequest.
This type accumulates headers, query parameters, cookies, and bodies before
building a URLRequest for execution by the HTTP client.
This class is NOT thread-safe. It contains mutable state without synchronization and should not be shared across multiple threads or modified concurrently.
-
The target URL for this HTTP request.
Declaration
Swift
public var url: String? { get set } -
Creates a new HTTP request with standard headers automatically injected.
Standard headers include:
x-requested-with: ping-sdkx-requested-platform: ios
Declaration
Swift
public init(logger: Logger = LogManager.logger)Parameters
loggerThe logger to use for this request. Defaults to
LogManager.logger. -
Adds a query parameter to the request URL.
Multiple calls with the same parameter name will add multiple values.
Declaration
Swift
public func setParameter(name: String, value: String)Parameters
nameThe parameter name.
valueThe parameter value.
-
Sets a header value for the request.
If the header already exists (case-insensitive comparison), it will be replaced.
Declaration
Swift
public func setHeader(name: String, value: String)Parameters
nameThe header name.
valueThe header value.
-
Adds a cookie to the request.
Declaration
Swift
public func setCookie(cookie: String)Parameters
cookieThe cookie string to add.
-
Adds multiple cookies to the request.
Declaration
Swift
public func setCookies(cookies: [String])Parameters
cookiesArray of cookie strings to add.
-
Configures the request as a GET request.
Clears any previously set body data.
Declaration
Swift
public func get() -
Configures the request as a POST request with a JSON body.
The dictionary will be serialized to JSON. Sets Content-Type to
application/json.Declaration
Swift
public func post(json: [String : Any] = [:])Parameters
jsonThe dictionary to serialize as JSON. Defaults to empty dictionary.
-
Configures the request as a PUT request with a JSON body.
The dictionary will be serialized to JSON. Sets Content-Type to
application/json.Declaration
Swift
public func put(json: [String : Any] = [:])Parameters
jsonThe dictionary to serialize as JSON. Defaults to empty dictionary.
-
Configures the request as a DELETE request with an optional JSON body.
The dictionary will be serialized to JSON. Sets Content-Type to
application/json.Declaration
Swift
public func delete(json: [String : Any] = [:])Parameters
jsonThe dictionary to serialize as JSON. Defaults to empty dictionary.
-
Configures the request as a POST request with a string body.
Declaration
Swift
public func post(contentType: String = NetworkConstants.contentTypeJSON, body: String)Parameters
contentTypeThe Content-Type header value. Defaults to
application/json.bodyThe string body to send.
-
Configures the request as a PUT request with a string body.
Declaration
Swift
public func put(contentType: String = NetworkConstants.contentTypeJSON, body: String)Parameters
contentTypeThe Content-Type header value. Defaults to
application/json.bodyThe string body to send.
-
Configures the request as a DELETE request with a string body.
Declaration
Swift
public func delete(contentType: String = NetworkConstants.contentTypeJSON, body: String)Parameters
contentTypeThe Content-Type header value. Defaults to
application/json.bodyThe string body to send.
-
Configures the request as a POST request with form-encoded data.
Multiple calls to
form()will accumulate parameters. Sets Content-Type toapplication/x-www-form-urlencoded.Declaration
Swift
public func form(parameters: [String : String])Parameters
parametersDictionary of form field names and values.
-
Sets the HTTP method directly.
Declaration
Swift
public func setMethod(_ method: HttpMethod)Parameters
methodThe HTTP method to use (GET, POST, PUT, DELETE, etc.).
-
Sets the request body directly as raw data.
Clears any JSON serialization errors.
Declaration
Swift
public func setBody(_ body: Data?)Parameters
bodyThe body data, or nil to clear the body.
-
Gets the currently configured HTTP method.
Declaration
Swift
public func getMethod() -> HttpMethodReturn Value
The configured HTTP method.
-
Gets a header value by name.
Performs case-insensitive header name lookup.
Declaration
Swift
public func getHeader(name: String) -> String?Parameters
nameThe header name to look up.
Return Value
The header value, or nil if not set.
-
Gets all headers as a dictionary.
Declaration
Swift
public func getHeaders() -> [String : String]Return Value
Dictionary of header names to values.
-
Builds a
URLRequestfrom the accumulated request state.This method constructs a complete
URLRequestby:- Using the URL with already-accumulated query parameters
Applying the HTTP method, headers, and body
Declaration
Swift
public func buildURLRequest() -> URLRequest?Return Value
A configured
URLRequest, ornilif the URL is invalid or JSON serialization failed.
View on GitHub