我正在尝试将一些数据发布到 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"
}
当我认为它应该回显“测试字符串”帖子时,此测试返回空字符串作为服务器的响应正文。
我理想地希望复制以下功能:
curl http://localhost/test.php -d 'Test string' -H 'Content-type:text/plain; charset=utf-8'
并使用服务器端 test.php 验证结果:
<?php
print (@file_get_contents('php://input'));
我做错了吗?或者我应该尝试另一个库?
您需要指定一个Content-Length
HTTP header,其值必须是原始发布数据的长度:
updateTest = Request { rqURI = updateURI
, rqMethod = POST
, rqHeaders = [ mkHeader HdrContentType "application/x-www-form-urlencoded"
, mkHeader HdrContentLength "8"
]
, rqBody = "raw data"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)