Why can't I return the ByteString from the handler to Yesod?

I am trying to return ByteStringfrom my function Handlerto Yesod:

getHomeR :: Handler ByteString
getHomeR = return "foo"

but I get this error:

/Users/maximiliantagher/Documents/Mercury/hs/mercury-web-backend/src/Application.hs:48:1: error:
    โ€ข No instance for (ToTypedContent ByteString)
        arising from a use of โ€˜yesodRunnerโ€™
    โ€ข In the expression:
        yesodRunner getHomeR env1404_axwe (Just HomeR) req1404_axwf
      In a case alternative:
          "GET"
            -> yesodRunner getHomeR env1404_axwe (Just HomeR) req1404_axwf
      In the expression:
        case Network.Wai.Internal.requestMethod req1404_axwf of {
          "GET"
            -> yesodRunner getHomeR env1404_axwe (Just HomeR) req1404_axwf
          _ -> yesodRunner
                 (void badMethod) env1404_axwe (Just HomeR) req1404_axwf }

Why does this happen, and why ByteStringdoes not it have an instance ToTypedContent?

+4
source share
1 answer

The class ToTypedContentdescribes what content-typedata is. Thus, types that have an associated content type (e.g., UTF 8 for Textor Value(JSON)) can have a natural instance ToTypedContent.

The problem with it ByteStringis that it describes any binary data - yours ByteStringcould be PNG, JPEG or something else, so it is not clear what type of content it should provide.

, octet-stream:

getHomeR :: Handler TypedContent
getHomeR = return $ TypedContent typeOctet $ toContent ("x" :: ByteString)

, (, image/jpeg JPEG).

TypedContent , , ToTypedContent ByteString

newtype Jpeg = Jpeg ByteString deriving (Show, ToContent)

instance ToTypedContent Jpeg where
    toTypedContent h = TypedContent "image/jpeg" (toContent h)
+7

Source: https://habr.com/ru/post/1690870/


All Articles