Haskell 中的 HTTP POST 内容

发布于 2024-09-06 04:07:42 字数 1285 浏览 5 评论 0原文

我正在尝试将一些数据发布到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-09-13 04:07:42

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

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

You need to specify a Content-Length HTTP header, whose value must be the length of the raw posted data:

updateTest = Request { rqURI     = updateURI
                     , rqMethod  = POST
                     , rqHeaders = [ mkHeader HdrContentType "application/x-www-form-urlencoded"
                                   , mkHeader HdrContentLength "8"
                                   ]
                     , rqBody    = "raw data"
                     }
蒲公英的约定 2024-09-13 04:07:42

对于http-conduit

{-# LANGUAGE OverloadedStrings #-}

import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L

main = do
  initReq <- parseUrl "http://localhost/test.php"

  let req = (flip urlEncodedBody) initReq $
             [ ("", "Test string")
--             ,
             ]

  response <- withManager $ httpLbs req

  L.putStr $ responseBody response

上例中的“测试字符串”在发布之前已进行urlEncoded。

您还可以手动设置方法、内容类型和请求正文。 api 与 http-enumerator 中的相同,一个很好的例子是:
https://stackoverflow.com/a/5614946

And with http-conduit:

{-# LANGUAGE OverloadedStrings #-}

import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L

main = do
  initReq <- parseUrl "http://localhost/test.php"

  let req = (flip urlEncodedBody) initReq $
             [ ("", "Test string")
--             ,
             ]

  response <- withManager $ httpLbs req

  L.putStr $ responseBody response

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文