Haskell 中的 HTTP POST 内容
我正在尝试将一些数据发布到 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'));
我做错了吗,还是应该尝试另一个库?
I'm trying to post some data to a server in Haskell and the server side is coming up empty.
I'm using the Network.HTTP library for the request.
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"
}
This test is returning the empty string as the response body from the server, when I think it should be echoing the "Test string" post.
I would ideally like to replicate the functionality of:
curl http://localhost/test.php -d 'Test string' -H 'Content-type:text/plain; charset=utf-8'
and am validating results with serverside test.php:
<?php
print (@file_get_contents('php://input'));
Am I doing this wrong or should I just be trying another library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要指定一个
Content-Length
HTTP 标头,其值必须是原始发布数据的长度:You need to specify a
Content-Length
HTTP header, whose value must be the length of the raw posted data:对于
http-conduit
:上例中的
“测试字符串”
在发布之前已进行urlEncoded。您还可以手动设置方法、内容类型和请求正文。 api 与 http-enumerator 中的相同,一个很好的例子是:
https://stackoverflow.com/a/5614946
And with
http-conduit
:The
"Test string"
, in the above example, is urlEncoded before being posted.You can also set the method, content-type, and request body manually. The api is the same as in http-enumerator a good example is:
https://stackoverflow.com/a/5614946