简体   繁体   English

WAI教程-没有针对(显示响应)的实例

[英]WAI tutorial - no instance for (Show Response)

Again the n00b here: trying Warp and WAI with the following code as in the documentation. 这里再次是n00b:使用文档中的以下代码尝试Warp和WAI。

{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)
app3 :: Application
app3 request respond = respond $ case rawPathInfo request of
    "/"     -> index
    "/raw/" -> plainIndex
    _       -> notFound

plainIndex :: Response
plainIndex = responseFile
    status200
    [("Content-Type", "text/plain")]
    "index.html"
    Nothing

notFound :: Response
notFound = responseLBS
    status404
    [("Content-Type", "text/plain")]
    "404 - Not Found"

Running plainIndex in GHCi returns: 在GHCi中运行plainIndex返回:

<interactive>:12:1: error:
    • No instance for (Show Response) arising from a use of ‘print’
    • In a stmt of an interactive GHCi command: print it
*Main> :r
[1 of 1] Compiling Main             ( WAI.hs, interpreted )

Two questions in one: can you help me out fixing this, and in exension to that: I am the only one frequently encountering issues like this when following documentation examples? 两个问题合二为一:您能帮助我解决此问题吗,以及除此之外:在遵循文档示例时,我是唯一经常遇到此类问题的人吗?

Running plainIndex in GHCi, GHCi tries to compute the Response and then print it in the terminal. 在GHCi中运行plainIndex ,GHCi尝试计算Response ,然后在终端中将其打印出来。 A Show instance defines how a given type should be represented as a String . Show实例定义如何将给定类型表示为String The library authors have chosen not to provide a Show instance for Response , probably to separate its representation from its interface. 库作者选择不为Response提供Show实例,可能是为了将其表示与其界面分开。

Various parts of a response have Show instances, so you can use accessors provided by Wai : 响应的各个部分都有Show实例,因此您可以使用Wai提供的访问器

> responseStatus plainIndex
> responseHeaders plainIndex

More documentation for Response . 有关响应的更多文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM