Haskell中的HTTP POST内容

amc*_*usl 8 networking haskell http

我正在尝试将一些数据发布到Haskell中的服务器,而服务器端则是空的.

我正在使用Network.HTTP库来处理请求.

module Main (main) where

import Network.URI (URI (..), parseURI, uriScheme, uriPath, uriQuery, uriFragment)
import Network.HTTP
import Network.TCP as TCP

main = do
         conn <- TCP.openStream "localhost" 80
         rawResponse <- sendHTTP conn updateTest
         body <- getResponseBody rawResponse
         if body == rqBody updateTest
           then print "test passed"
           else print (body ++ " != " ++ (rqBody updateTest))

updateURI = case parseURI "http://localhost/test.php" of
                  Just u -> u

updateTest = Request { rqURI = updateURI :: URI
                     , rqMethod = POST :: RequestMethod
                     , rqHeaders = [ Header HdrContentType   "text/plain; charset=utf-8"
                                   ] :: [Header]
                     , rqBody = "Test string"
                     }
Run Code Online (Sandbox Code Playgroud)

这个测试是从服务器返回空字符串作为响应主体,当我认为它应该回显"测试字符串"帖子.

我理想地希望复制以下功能:

curl http://localhost/test.php -d 'Test string' -H 'Content-type:text/plain; charset=utf-8'
Run Code Online (Sandbox Code Playgroud)

并使用serverside test.php验证结果:

<?php
print (@file_get_contents('php://input'));
Run Code Online (Sandbox Code Playgroud)

我这样做错了还是我应该尝试另一个图书馆?

Ion*_*tan 4

您需要指定一个Content-LengthHTTP 标头,其值必须是原始发布数据的长度:

updateTest = Request { rqURI     = updateURI
                     , rqMethod  = POST
                     , rqHeaders = [ mkHeader HdrContentType "application/x-www-form-urlencoded"
                                   , mkHeader HdrContentLength "8"
                                   ]
                     , rqBody    = "raw data"
                     }
Run Code Online (Sandbox Code Playgroud)