Skip to content

HTTPBody

Josh Wright edited this page Jan 14, 2021 · 3 revisions

HTTPBody

The contents of an HTTP request or response.

public struct HTTPBody: ExpressibleByStringLiteral

Inheritance

ExpressibleByStringLiteral

Initializers

init(buffer:mimeType:)

Creates a new body from a binary NIO.ByteBuffer.

public init(buffer: ByteBuffer, mimeType: MIMEType? = nil)

Parameters

  • buffer: The buffer holding the data in the body.
  • mimeType: The MIME type of data in the body.

init(text:mimeType:)

Creates a new body containing the text with MIME type text/plain.

public init(text: String, mimeType: MIMEType = .plainText)

Parameters

  • text: The string contents of the body.
  • mimeType: The media type of this text. Defaults to .plainText ("text/plain").

init(data:mimeType:)

Creates a new body from a binary Foundation.Data.

public init(data: Data, mimeType: MIMEType? = nil)

Parameters

  • data: The data in the body.
  • mimeType: The MIME type of the body.

init(json:encoder:)

Creates a body with a JSON object.

public init<E: Encodable>(json: E, encoder: JSONEncoder = Response.defaultJSONEncoder) throws

Parameters

  • json: The object to encode into the body.
  • encoder: A customer encoder to encoder the JSON with. Defaults to Response.defaultJSONEncoder.

Throws

Any error thrown during encoding.

init(stringLiteral:)

Create a body via a string literal.

public init(stringLiteral value: String)

Parameters

  • value: The string literal contents of the body.

Properties

buffer

The binary data in this body.

let buffer: ByteBuffer

mimeType

The mime type of the data stored in this body. Used to set the content-type header when sending back a response.

let mimeType: MIMEType?

data

The contents of this body.

var data: Data

Methods

decodeString(with:)

Decodes the body as a String.

public func decodeString(with encoding: String.Encoding = .utf8) -> String?

Parameters

  • encoding: The String.Encoding value to decode with. Defaults to .utf8.

Returns

The string decoded from the contents of this body.

decodeJSONDictionary()

Decodes the body as a JSON dictionary.

public func decodeJSONDictionary() throws -> [String: Any]?

Throws

If there's a error decoding the dictionary.

Returns

The dictionary decoded from the contents of this body.

decodeJSON(as:with:)

Decodes the body as JSON into the provided Decodable type.

public func decodeJSON<D: Decodable>(as type: D.Type = D.self, with decoder: JSONDecoder = Request.defaultJSONDecoder) throws -> D

Parameters

  • type: The Decodable type to which the body should be decoded.
  • decoder: The Decoder with which to decode. Defaults to Request.defaultJSONEncoder.

Throws

Any errors encountered during decoding.

Returns

The decoded object of type type.

Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally