在Haskell的Wai / Warp中处理POST请求

时间:2017-04-12 12:47:58

标签: haskell haskell-wai haskell-warp

我有一个wai / warp应用程序。我该如何处理邮寄申请?我有这个:

app :: Application
app request respond = 
    respond $ case rawPathInfo request of
        "/"  -> responseFile status200 ............
        "/some_post_req/" -> .....

如何指定some_post_req必须是POST?

2 个答案:

答案 0 :(得分:1)

应该像将s=requests.Session() proxy_url=os.environ['HTTPS_PROXY'] s.proxies["https"]=proxy_url # have to specify proxy here because env variable is only detected by httplib code #while we need to trigger requests' proxy logic that acts earlier # "https" means any https host. Since a Session persists cookies, #it's meaningless to make requests to multiple hosts through it anyway. pm=s.get_adapter("https://").proxy_manager_for(proxy_url) pm.proxy_headers['Host']="host.com" del pm,proxy_url <...> s.get('https://host.com') Network.Wai.requestMethod的结果进行比较一样简单:

Network.Wai.methodPost

由于app request respond | requestMethod request == methodPost = respond $ case rawPathInfo request of {- handle POST routes -} | otherwise = {- handle other request methods -} methodPost和&amp; c。都有常量,您可以使用它们,但methodGetMethod的别名,所以你也可以使用ByteString扩展程序:

OverloadedStrings

然后与字符串文字进行比较:

{-# LANGUAGE OverloadedStrings #-}

或模式匹配:

requestMethod request == "POST"

答案 1 :(得分:0)

wai-extra包中的

parseRequestBody可让您从请求正文中获取所需数据:

(params, files) <- parseRequestBody lbsBackEnd request

但这没有说明请求必须是POST请求。请注意,wai可能会非常低级,并且有更高级别的包。

servant作为一个包,可以让您在类型级别定义API。使用servant,您绝对可以指定您期望的HTTP动词。见here