RequestEncoder

public class RequestEncoder
extension RequestEncoder: TopLevelEncoder

A class responsible for encoding HTTP requests into various formats.

The RequestEncoder class provides functionality to encode HTTP requests into different formats, including JSON, Form URL Encoded, Plain, and XML. It uses the RequestEncoding encoder to perform the encoding process.

  • Initializes a new instance of RequestEncoder with the specified base URL.

    Declaration

    Swift

    public init(baseURL: URL)

    Parameters

    baseURL

    The base URL for the requests.

  • Encodes an HTTP request into a URLRequest.

    Throws

    An error if the encoding process fails.

    Example usage:

    let encoder = RequestEncoder(baseURL: URL(string: "https://api.example.com")!)
    let urlRequest = try encoder.encode(myRequest)
    

    Declaration

    Swift

    public func encode<Request>(_ request: Request) throws -> URLRequest where Request : Encodable

    Parameters

    request

    The request to encode.

    Return Value

    The encoded URLRequest.

JSON

  • Encodes an HTTP request with a JSON body into a URLRequest.

    Throws

    An error if the encoding process fails.

    Example usage:

    let encoder = RequestEncoder(baseURL: URL(string: "https://api.example.com")!)
    let urlRequest = try encoder.encodeJson(request: myJsonRequest)
    

    Declaration

    Swift

    public func encodeJson<Request>(request: Request) throws -> URLRequest where Request : JSONBodyProvider, Request : JSONFormatProvider, Request : Encodable

    Parameters

    request

    The request to encode.

    Return Value

    The encoded URLRequest.

Form URL Encoded

Plain

  • Encodes an HTTP request with a plain text body into a URLRequest.

    Throws

    An error if the encoding process fails.

    Example usage:

    let encoder = RequestEncoder(baseURL: URL(string: "https://api.example.com")!)
    let urlRequest = try encoder.encodePlain(request: myPlainRequest)
    

    Declaration

    Swift

    public func encodePlain<Request>(request: Request) throws -> URLRequest where Request : PlainBodyProvider, Request : PlainFormatProvider, Request : Encodable

    Parameters

    request

    The request to encode.

    Return Value

    The encoded URLRequest.

XML

  • Encodes an HTTP request with an XML body into a URLRequest.

    Throws

    An error if the encoding process fails.

    Example usage:

    let encoder = RequestEncoder(baseURL: URL(string: "https://api.example.com")!)
    let urlRequest = try encoder.encodeXML(request: myXmlRequest)
    

    Declaration

    Swift

    public func encodeXML<Request>(request: Request) throws -> URLRequest where Request : XMLBodyProvider, Request : XMLFormatProvider, Request : Encodable

    Parameters

    request

    The request to encode.

    Return Value

    The encoded URLRequest.