HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulehttp-api-data-0.6.1Haskell2010

Web.HttpApiData

Convert Haskell values to and from HTTP API data such as URL pieces, headers and query parameters.

  • 1 type
  • 2 classes
  • 18 values

Examples

0 declarations

Booleans:

Example3 expressions
toUrlPiece True"true"parseUrlPiece "false" :: Either Text BoolRight FalseparseUrlPieces ["true", "false", "undefined"] :: Either Text [Bool]Left "could not parse: `undefined'"

Numbers:

Example4 expressions
toQueryParam 45.2"45.2"parseQueryParam "452" :: Either Text IntRight 452toQueryParams [1..5] :: [Text]["1","2","3","4","5"]parseQueryParams ["127", "255"] :: Either Text [Int8]Left "out of bounds: `255' (should be between -128 and 127)"

Strings:

Example2 expressions
toHeader "hello""hello"parseHeader "world" :: Either Text StringRight "world"

Calendar day:

Example2 expressions
toQueryParam (fromGregorian 2015 10 03)"2015-10-03"toGregorian <$> parseQueryParam "2016-12-01"Right (2016,12,1)

Classes

2 declarations
classclass ToHttpApiData a where
#

Convert value to HTTP API data.

WARNING: Do not derive this using DeriveAnyClass as the generated instance will loop indefinitely.

Methods

Instances52ToHttpApiData, …
  • ToHttpApiData SetCookieDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData

    Note: this instance works correctly for alphanumeric name and value

    Example2 expressions
    let Right c = parseUrlPiece "SESSID=r2t5uvjq435r4q7ib3vtdjq120" :: Either Text SetCookietoUrlPiece c"SESSID=r2t5uvjq435r4q7ib3vtdjq120"
    Example1 expression
    toHeader c"SESSID=r2t5uvjq435r4q7ib3vtdjq120"
  • ToHttpApiData IntegerDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData NaturalDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData StringDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData VoidDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData AllDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData AnyDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData VersionDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece (Version [1, 2, 3] [])"1.2.3"
  • ToHttpApiData Int16Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData Int32Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData Int64Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData Int8Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData Word16Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData Word32Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData Word64Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData Word8Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData BoolDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData CharDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData DoubleDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData FloatDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData IntDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData OrderingDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData WordDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData TextDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData TextDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData DayDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece (fromGregorian 2015 10 03)"2015-10-03"
  • ToHttpApiData MonthDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example2 expressions
    import Data.Time.Calendar.Month.Compat (Month (..))MkMonth 244822040-03
    Example1 expression
    toUrlPiece $ MkMonth 24482"2040-03"
  • ToHttpApiData QuarterDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example2 expressions
    import Data.Time.Calendar.Quarter.Compat (Quarter (..))MkQuarter 80402010-Q1
    Example1 expression
    toUrlPiece $ MkQuarter 8040"2010-q1"
  • ToHttpApiData QuarterOfYearDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece Q4"q4"
  • ToHttpApiData DayOfWeekDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece Monday"monday"
  • ToHttpApiData NominalDiffTimeDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData UTCTimeDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece $ UTCTime (fromGregorian 2015 10 03) 864.5"2015-10-03T00:14:24.500Z"
  • ToHttpApiData LocalTimeDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece $ LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 21.687)"2015-10-03T14:55:21.687"
  • ToHttpApiData TimeOfDayDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece $ TimeOfDay 14 55 23.1"14:55:23.100"
  • ToHttpApiData ZonedTimeDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece $ ZonedTime (LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 51.001)) utc"2015-10-03T14:55:51.001Z"
    Example1 expression
    toUrlPiece $ ZonedTime (LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 51.001)) (TimeZone 120 True "EET")"2015-10-03T14:55:51.001+02:00"
  • ToHttpApiData UUIDDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData ()Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece ()"_"
  • ToHttpApiData a => ToHttpApiData (First a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Last a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Max a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Min a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Identity a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (First a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Last a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Dual a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Product a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Sum a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Maybe a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example1 expression
    toUrlPiece (Just "Hello")"just Hello"
  • HasResolution a => ToHttpApiData (Fixed a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData

    Note: this instance is not polykinded

  • (ToHttpApiData a, ToHttpApiData b) => ToHttpApiData (Either a b)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
    Example2 expressions
    toUrlPiece (Left "err" :: Either String Int)"left err"toUrlPiece (Right 3 :: Either String Int)"right 3"
  • ToHttpApiData a => ToHttpApiData (Const a b)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData
  • ToHttpApiData a => ToHttpApiData (Tagged b a)Defined in http-api-data-0.6.1 · Web.Internal.HttpApiData

    Note: this instance is not polykinded

classclass FromHttpApiData a where
#

Parse value from HTTP API data.

WARNING: Do not derive this using DeriveAnyClass as the generated instance will loop indefinitely.

Methods

Instances53FromHttpApiData, …

Maybe parsers

3 declarations

Prefix parsers

3 declarations

Case insensitive.

Parse given text case insensitive and then parse the rest of the input using parseUrlPiece.

Example2 expressions
parseUrlPieceWithPrefix "Just " "just 10" :: Either Text IntRight 10parseUrlPieceWithPrefix "Left " "left" :: Either Text BoolLeft "could not parse: `left'"

This can be used to implement FromHttpApiData for single field constructors:

Example3 expressions
data Foo = Foo Int deriving (Show)instance FromHttpApiData Foo where parseUrlPiece s = Foo <$> parseUrlPieceWithPrefix "Foo " sparseUrlPiece "foo 1" :: Either Text FooRight (Foo 1)

Parse given bytestring then parse the rest of the input using parseHeader.

data BasicAuthToken = BasicAuthToken Text deriving (Show)

instance FromHttpApiData BasicAuthToken where
  parseHeader h     = BasicAuthToken <$> parseHeaderWithPrefix "Basic " h
  parseQueryParam p = BasicAuthToken <$> parseQueryParam p
Example1 expression
parseHeader "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" :: Either Text BasicAuthTokenRight (BasicAuthToken "QWxhZGRpbjpvcGVuIHNlc2FtZQ==")

Multiple URL pieces

2 declarations
valuetoUrlPieces :: (Functor t, ToHttpApiData a) => t a -> t Text
#

Convert multiple values to a list of URL pieces.

Example1 expression
toUrlPieces [1, 2, 3] :: [Text]["1","2","3"]
valueparseUrlPieces
  1. :: (Traversable t, FromHttpApiData a)
  2. => t Text
  3. -> Either Text (t a)
#

Parse multiple URL pieces.

Example2 expressions
parseUrlPieces ["true", "false"] :: Either Text [Bool]Right [True,False]parseUrlPieces ["123", "hello", "world"] :: Either Text [Int]Left "could not parse: `hello' (input does not start with a digit)"

Multiple query params

2 declarations
valuetoQueryParams :: (Functor t, ToHttpApiData a) => t a -> t Text
#

Convert multiple values to a list of query parameter values.

Example1 expression
toQueryParams [fromGregorian 2015 10 03, fromGregorian 2015 12 01] :: [Text]["2015-10-03","2015-12-01"]

Parse multiple query parameters.

Example2 expressions
parseQueryParams ["1", "2", "3"] :: Either Text [Int]Right [1,2,3]parseQueryParams ["64", "128", "256"] :: Either Text [Word8]Left "out of bounds: `256' (should be between 0 and 255)"

Parsers for Bounded Enums

6 declarations
valueparseBoundedTextData :: (Show a, Bounded a, Enum a) => Text -> Either Text a
#

Case insensitive.

Parse values case insensitively based on Show instance.

Example2 expressions
parseBoundedTextData "true" :: Either Text BoolRight TrueparseBoundedTextData "FALSE" :: Either Text BoolRight False

This can be used as a default implementation for enumeration types:

Example3 expressions
data MyData = Foo | Bar | Baz deriving (Show, Bounded, Enum)instance FromHttpApiData MyData where parseUrlPiece = parseBoundedTextDataparseUrlPiece "foo" :: Either Text MyDataRight Foo

Lenient data

1 declaration
newtypenewtype LenientData a
#

Lenient parameters. FromHttpApiData combinators always return Right.

Instances9Functor, Foldable, Traversable, Eq, Data, Ord, …

Other helpers

2 declarations
valueshowTextData :: Show a => a -> Text
#

Lower case.

Convert to URL piece using Show instance. The result is always lower cased.

Example1 expression
showTextData True"true"

This can be used as a default implementation for enumeration types:

Example3 expressions
data MyData = Foo | Bar | Baz deriving (Show)instance ToHttpApiData MyData where toUrlPiece = showTextDatatoUrlPiece Foo"foo"
valuereadTextData :: Read a => Text -> Either Text a
#

Parse URL piece using Read instance.

Use for types which do not involve letters:

Example1 expression
readTextData "1991-06-02" :: Either Text DayRight 1991-06-02

This parser is case sensitive and will not match showTextData in presence of letters:

Example1 expression
readTextData (showTextData True) :: Either Text BoolLeft "could not parse: `true'"

See parseBoundedTextData.