2017-12-15 25 views
4

나는 Yesod 내 Handler 기능에서 ByteString를 반환하기 위해 노력하고있어Yesod의 핸들러에서 ByteString을 반환 할 수없는 이유는 무엇입니까?</p> <pre><code>getHomeR :: Handler ByteString getHomeR = return "foo" </code></pre> <p>하지만이 오류 받고 있어요 :

/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 } 

왜 이런 일이하고 ByteString하지 않는 이유가 a ToTypedContent 인스턴스?

답변

7

ToTypedContent 클래스는 content-type의 데이터를 설명합니다. 연결된 콘텐츠 유형 (예 : Text 또는 Value (UTF)의 경우 UTF 8)은 자연어 ToTypedContent 인스턴스를 가질 수 있습니다.

ByteString의 문제점은 ByteString이 PNG, JPEG 또는 기타 형식 일 수있는 모든 이진 데이터를 설명하기 때문에 콘텐츠 유형을 분명히 알 수 없습니다.

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

하지만 당신은 (JPEG에 대한 예를 들어, image/jpeg) 가능하면 더 나은 콘텐츠 형식에 대한 시도해야합니다 :

당신이 정말 바이너리 데이터를 반환하려면

octet-stream 콘텐츠 형식이 적합합니다. 이 경우

, 당신은 위와 같이 수동으로 TypedContent를 사용하거나 ByteString

newtype Jpeg = Jpeg ByteString deriving (Show, ToContent) 

instance ToTypedContent Jpeg where 
    toTypedContent h = TypedContent "image/jpeg" (toContent h) 
통해 newtype이란에 대한 ToTypedContent의 자신의 인스턴스를 작성할 수 있습니다