Module

HTTPure.Body

Package
purescript-httpure
Repository
citizennet/purescript-httpure

#Body Source

class Body b  where

Types that implement the Body class can be used as a body to an HTTPure response, and can be used with all the response helpers.

Members

  • defaultHeaders :: b -> Effect Headers

    Return any default headers that need to be sent with this body type, things like Content-Type, Content-Length, and Transfer-Encoding. Note that any headers passed in a response helper such as ok' will take precedence over these.

  • write :: b -> Response -> Aff Unit

    Given a body value and a Node HTTP Response value, write the body value to the Node response.

Instances

  • Body String

    The instance for String will convert the string to a buffer first in order to determine it's additional headers. This is to ensure that the Content-Length header properly accounts for UTF-8 characters in the string. Writing is simply implemented by writing the string to the response stream and closing the response stream.

  • Body Buffer

    The instance for Buffer is trivial--we add a Content-Length header using Buffer.size, and to send the response, we just write the buffer to the stream and end the stream.

  • (TypeEquals (Stream r) (Readable s)) => Body (Stream r)

    This instance can be used to send chunked data. Here, we add a Transfer-Encoding header to indicate chunked data. To write the data, we simply pipe the newtype-wrapped Stream to the response.

#RequestBody Source

type RequestBody = { buffer :: Ref (Maybe Buffer), stream :: Readable (), string :: Ref (Maybe String) }

#read Source

read :: Request -> Effect RequestBody

Read the body Readable stream out of the incoming request

#toBuffer Source

toBuffer :: RequestBody -> Aff Buffer

Turn RequestBody into a Buffer

This drains the Readable stream in RequestBody for the first time and returns cached result from then on.

#toStream Source

toStream :: RequestBody -> Readable ()

Return the Readable stream directly from RequestBody

#toString Source

toString :: RequestBody -> Aff String

Turn RequestBody into a String

This drains the Readable stream in RequestBody for the first time and returns cached result from then on.