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.
-
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
nameThe parameter name.
valueThe 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
nameThe header name (case-insensitive).
valueThe header value.
-
Adds a cookie to the request.
Declaration
Swift
func setCookie(cookie: String)Parameters
cookieThe cookie string to add.
-
Adds multiple cookies to the request.
Declaration
Swift
func setCookies(cookies: [String])Parameters
cookiesArray 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
jsonDictionary to serialize as JSON.
-
Configures the request as a PUT request with JSON body.
Declaration
Swift
func put(json: [String : Any])Parameters
jsonDictionary to serialize as JSON.
-
Configures the request as a DELETE request with JSON body.
Declaration
Swift
func delete(json: [String : Any])Parameters
jsonDictionary 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
contentTypeContent-Type header value. Defaults to application/json to align with Android.
bodyString 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
contentTypeContent-Type header value. Defaults to application/json to align with Android.
bodyString 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
contentTypeContent-Type header value. Defaults to application/json to align with Android.
bodyString 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
parametersDictionary of form field names and values.
-
Sets the HTTP method directly.
Declaration
Swift
func setMethod(_ method: HttpMethod)Parameters
methodThe HTTP method to use.
-
Sets the request body directly.
Declaration
Swift
func setBody(_ body: Data?)Parameters
bodyThe body data.
-
Gets the current HTTP method.
Declaration
Swift
func getMethod() -> HttpMethodReturn Value
The configured HTTP method.
-
Gets a header value by name.
Declaration
Swift
func getHeader(name: String) -> String?Parameters
nameThe 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.
View on GitHub