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.

  • url

    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-sdk
    • x-requested-platform: ios

    Declaration

    Swift

    public init(logger: Logger = LogManager.logger)

    Parameters

    logger

    The 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

    name

    The parameter name.

    value

    The 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

    name

    The header name.

    value

    The header value.

  • Adds a cookie to the request.

    Declaration

    Swift

    public func setCookie(cookie: String)

    Parameters

    cookie

    The cookie string to add.

  • Adds multiple cookies to the request.

    Declaration

    Swift

    public func setCookies(cookies: [String])

    Parameters

    cookies

    Array 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

    json

    The 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

    json

    The 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

    json

    The 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

    contentType

    The Content-Type header value. Defaults to application/json.

    body

    The 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

    contentType

    The Content-Type header value. Defaults to application/json.

    body

    The 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

    contentType

    The Content-Type header value. Defaults to application/json.

    body

    The 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 to application/x-www-form-urlencoded.

    Declaration

    Swift

    public func form(parameters: [String : String])

    Parameters

    parameters

    Dictionary of form field names and values.

  • Sets the HTTP method directly.

    Declaration

    Swift

    public func setMethod(_ method: HttpMethod)

    Parameters

    method

    The 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

    body

    The body data, or nil to clear the body.

  • Gets the currently configured HTTP method.

    Declaration

    Swift

    public func getMethod() -> HttpMethod

    Return 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

    name

    The 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 URLRequest from the accumulated request state.

    This method constructs a complete URLRequest by:

    • 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, or nil if the URL is invalid or JSON serialization failed.