Haskell中的HTTP POST内容 [英] HTTP POST contents in Haskell

查看:135
本文介绍了Haskell中的HTTP POST内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用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 .openStreamlocalhost80
rawResponse< - sendHTTP conn updateTest
body< - getResponseBody rawResponse
if body == rqBody updateTest
然后打印test passed
else print(body ++!=++(rqBody updateTest))

updateURI = case parseURIhttp://localhost/test.php
只要你 - > ; u

updateTest = Request {rqURI = updateURI :: URI
,rqMethod = POST :: RequestMethod
,rqHeaders = [Header HdrContentTypetext / plain; charset = utf-8
:: [Header]
,rqBody =Test string
}

这个测试从服务器返回空字符串作为响应体,当我认为它应该回显Test string后。



我希望能够复制以下功能:

  curl http://localhost/test.php -d'Test string' -H'内容类型:text / plain; charset = utf-8'

以及使用serverside test.php验证结果:

 <?php 
print(@file_get_contents('php:// input'));

我是在做这个错误还是应该试试其他库?

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

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ c $ update $ [mkHeader HdrContentTypeapplication / x-www-form-urlencoded
,mkHeader HdrContentLength8
]
,rqBody =原始数据
}


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?

解决方案

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"
                     }

这篇关于Haskell中的HTTP POST内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆