Vapor Documentation Beta

Structure URLEncoded​Form​Encoder

public struct URLEncodedFormEncoder: ContentEncoder, URLQueryEncoder

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".

%3 URLEncodedFormEncoder URLEncodedFormEncoder ContentEncoder ContentEncoder URLEncodedFormEncoder->ContentEncoder URLQueryEncoder URLQueryEncoder URLEncodedFormEncoder->URLQueryEncoder

Nested Types

URLEncodedFormEncoder.Configuration

Used to capture URLForm Coding Configuration used for encoding.

Conforms To

ContentEncoder
URLQueryEncoder

Initializers

init(configuration:​)

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

Create a new URLEncodedFormEncoder.

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

Parameters

configuration Configuration

Defines how encoding is done see URLEncodedFormCodingConfig for more information

Methods

encode(_:​to:​headers:​)

public func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders) throws where E: Encodable

ContentEncoder conformance.

encode(_:​to:​)

public func encode<E>(_ encodable: E, to url: inout URI) throws where E: Encodable

URLContentEncoder conformance.

encode(_:​)

public func encode<E>(_ encodable: E) throws -> String 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"

Parameters

encodable E

Generic Encodable object (E) to encode.

configuration

Overwrides the coding config for this encoding call.

Throws

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

Returns

Encoded Data