views:

79

answers:

1

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?

+1  A: 

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"
                     }
Ionuț G. Stan