URLEncodedFormEncoder

public struct URLEncodedFormEncoder

Encodes Encodable instances to application/x-www-form-urlencoded data.

print(user) /// User
let data = try URLEncodedFormEncoder().encode(user)
print(data) /// Data

URL-encoded forms are commonly used by websites to send form data via POST requests. This encoding is relatively efficient for small amounts of data but must be percent-encoded. multipart/form-data is more efficient for sending large data blobs like files.

See Mozilla’s docs for more information about url-encoded forms. NOTE: This implementation of the encoder does not support encoding booleans to “flags”.

  • Used to capture URLForm Coding Configuration used for encoding.

    See more

    Declaration

    Swift

    public struct Configuration
  • Create a new URLEncodedFormEncoder.

       ContentConfiguration.global.use(urlEncoder: URLEncodedFormEncoder(bracketsAsArray: true, flagsAsBool: true, arraySeparator: nil))
    

    Declaration

    Swift

    public init(
        configuration: Configuration = .init()
    )

    Parameters

    configuration

    Defines how encoding is done see URLEncodedFormCodingConfig for more information

  • URLContentEncoder conformance.

    Declaration

    Swift

    public func encode<E>(_ encodable: E, to url: inout URL) throws
        where E: Encodable
  • Encodes the supplied Encodable object to Data.

    print(user) // User
    let data = try URLEncodedFormEncoder().encode(user)
    print(data) // "name=Vapor&age=3"
    

    Throws

    Any error that may occur while attempting to encode the specified type.

    Declaration

    Swift

    public func encode<E>(_ encodable: E) throws -> String
        where E: Encodable

    Parameters

    encodable

    Generic Encodable object (E) to encode.

    configuration

    Overwrides the coding config for this encoding call.

    Return Value

    Encoded Data