HttpRequest

public protocol HttpRequest : AnyObject, Sendable

Protocol defining the contract for building HTTP requests.

HttpRequest provides a fluent interface for configuring HTTP requests including URL, headers, query parameters, cookies, and request body.

This protocol requires Sendable conformance to allow passing request instances across async boundaries. However, implementations are not required to be thread-safe during configuration. Request builders are designed to be used within a single async context (typically a builder closure) and should not be shared or modified concurrently.

  • url

    The URL for this request.

    Declaration

    Swift

    var url: String? { get set }
  • Adds a query parameter to the request URL.

    Multiple calls with the same parameter name will add multiple values.

    Declaration

    Swift

    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, it will be replaced.

    Declaration

    Swift

    func setHeader(name: String, value: String)

    Parameters

    name

    The header name (case-insensitive).

    value

    The header value.

  • Adds a cookie to the request.

    Declaration

    Swift

    func setCookie(cookie: String)

    Parameters

    cookie

    The cookie string to add.

  • Adds multiple cookies to the request.

    Declaration

    Swift

    func setCookies(cookies: [String])

    Parameters

    cookies

    Array of cookie strings.

  • Configures the request as a GET request.

    Declaration

    Swift

    func get()
  • Configures the request as a POST request with JSON body.

    Declaration

    Swift

    func post(json: [String : Any])

    Parameters

    json

    Dictionary to serialize as JSON.

  • Configures the request as a PUT request with JSON body.

    Declaration

    Swift

    func put(json: [String : Any])

    Parameters

    json

    Dictionary to serialize as JSON.

  • Configures the request as a DELETE request with JSON body.

    Declaration

    Swift

    func delete(json: [String : Any])

    Parameters

    json

    Dictionary to serialize as JSON.

  • Configures the request as a POST request with a raw string body.

    Declaration

    Swift

    func post(contentType: String, body: String)

    Parameters

    contentType

    Content-Type header value. Defaults to application/json to align with Android.

    body

    String payload to encode as UTF-8.

  • Configures the request as a PUT request with a raw string body.

    Declaration

    Swift

    func put(contentType: String, body: String)

    Parameters

    contentType

    Content-Type header value. Defaults to application/json to align with Android.

    body

    String payload to encode as UTF-8.

  • Configures the request as a DELETE request with a raw string body.

    Declaration

    Swift

    func delete(contentType: String, body: String)

    Parameters

    contentType

    Content-Type header value. Defaults to application/json to align with Android.

    body

    String payload to encode as UTF-8.

  • Configures the request as a POST with form-encoded data.

    Multiple calls to form() will accumulate parameters.

    Declaration

    Swift

    func form(parameters: [String : String])

    Parameters

    parameters

    Dictionary of form field names and values.

  • Sets the HTTP method directly.

    Declaration

    Swift

    func setMethod(_ method: HttpMethod)

    Parameters

    method

    The HTTP method to use.

  • Sets the request body directly.

    Declaration

    Swift

    func setBody(_ body: Data?)

    Parameters

    body

    The body data.

  • Gets the current HTTP method.

    Declaration

    Swift

    func getMethod() -> HttpMethod

    Return Value

    The configured HTTP method.

  • Gets a header value by name.

    Declaration

    Swift

    func getHeader(name: String) -> String?

    Parameters

    name

    The header name (case-insensitive).

    Return Value

    The header value, or nil if not set.

  • Gets all headers as a dictionary.

    Declaration

    Swift

    func getHeaders() -> [String : String]

    Return Value

    Dictionary of header names to values.