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"
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With