URLEncodedFormEncoder

public final class URLEncodedFormEncoder

An object that encodes instances into URL-encoded query strings.

There is no published specification for how to encode collection types. By default, the convention of appending [] to the key for array values (foo[]=1&foo[]=2), and appending the key surrounded by square brackets for nested dictionary values (foo[bar]=baz) is used. Optionally, ArrayEncoding can be used to omit the square brackets appended to array keys.

BoolEncoding can be used to configure how Bool values are encoded. The default behavior is to encode true as 1 and false as 0.

SpaceEncoding can be used to configure how spaces are encoded. Modern encodings use percent replacement (%20), while older encoding may expect spaces to be replaced with +.

This type is largely based on Vapor’s url-encoded-form project.

  • Configures how Bool parameters are encoded.

    See more

    Declaration

    Swift

    public enum BoolEncoding
  • Configures how Array parameters are encoded.

    See more

    Declaration

    Swift

    public enum ArrayEncoding
  • Configures how spaces are encoded.

    See more

    Declaration

    Swift

    public enum SpaceEncoding
  • Declaration

    Swift

    public enum Error : Swift.Error
  • The ArrayEncoding to use.

    Declaration

    Swift

    public let arrayEncoding: ArrayEncoding
  • The BoolEncoding to use.

    Declaration

    Swift

    public let boolEncoding: BoolEncoding
  • The SpaceEncoding to use.

    Declaration

    Swift

    public let spaceEncoding: SpaceEncoding
  • The CharacterSet of allowed characters.

    Declaration

    Swift

    public var allowedCharacters: CharacterSet
  • Creates an instance from the supplied parameters.

    Declaration

    Swift

    public init(arrayEncoding: ArrayEncoding = .brackets,
                boolEncoding: BoolEncoding = .numeric,
                spaceEncoding: SpaceEncoding = .percentEscaped,
                allowedCharacters: CharacterSet = .afURLQueryAllowed)

    Parameters

    arrayEncoding

    The ArrayEncoding instance. Defaults to .brackets.

    boolEncoding

    The BoolEncoding instance. Defaults to .numeric.

    spaceEncoding

    The SpaceEncoding instance. Defaults to .percentEscaped.

    allowedCharacters

    The CharacterSet of allowed (non-escaped) characters. Defaults to .afURLQueryAllowed.

  • Encodes the value as a URL form encoded String.

    Throws

    An Error or EncodingError instance if encoding fails.

    Declaration

    Swift

    public func encode(_ value: Encodable) throws -> String

    Parameters

    value

    The Encodable value.`

    Return Value

    The encoded String.

  • Encodes the value as Data. This is performed by first creating an encoded String and then returning the .utf8 data.

    Throws

    An Error or EncodingError instance if encoding fails.

    Declaration

    Swift

    public func encode(_ value: Encodable) throws -> Data

    Parameters

    value

    The Encodable value.

    Return Value

    The encoded Data.