Hi
I’m trying to get HTTP POST working with an Open AT application.
I took the standard “HTTP GET” sample, changed the request type to HTTP POST and added some code in http_ClientTestDataHandler() to write the actual data.
I’ve tried three different web hosts (2 x linux Apache and one x IIS7) with a test PHP script that prints out all the GET/POST variables. I have tested my php script using a simple HTML form which posts things to the same test script and it works properly on all hosts - showing a list of all the GET/POST variables.
When I try it from the Wavecom modem (Fastrax Supreme with firmware R74) I get a response of 200 (OK) but the script doesn’t show as having received any data.
I’ve tried lots of variations
- using HTTP/1.0 and HTTP/1.1
- setting/leaving out the content length
- adding extra “host” headers
If I get the content length wrong the web server returns an error. On some web hosts using HTTP/1.1 makes Snort (intrusion prevention s/w for linux boxes) blacklist the IP address for 24 hours because it thinks I’m doing a chunked encoding attack. On other servers HTTP/1.1 works fine. I’ve managed to get an “OK” response on all 4 servers but I never get any data to my PHP script.
I have no idea what I am doing wrong! I feel it should be simple to post some data to a form but I’ve spent days on this already.
The sample HTML form is at (link removed)
The destination php page is at: (link removed).
The code I have is as follows:
In http_ClientTestDataHandler()
case WIP_CEV_WRITE:
TRACE(( 4, "http_ClientTestDataHandler: WIP_CEV_WRITE\n" ));
adl_atSendResponse ( ADL_AT_UNS, "\r\nhttp_ClientTestDataHandler: WIP_CEV_WRITE\r\n");
wm_sprintf(info, "abcef=12345678901234"); // 20 bytes long
BytesWritten = wip_write( pHttpClientCtx->DataChannel, info, 20);
if(BytesWritten < 0)
{/* error */
//wip_debug( "HTTP write error %i\n", BytesWritten);
adl_atSendResponse ( ADL_AT_UNS, "\r\nwip_write error\r\n" );
return;
}
else
{
TRACE(( 4, "http_ClientTestDataHandler: bytes %d written\n", BytesWritten ));
wm_sprintf( info, "\r\nhttp_ClientTestDataHandler: %d bytes written\r\n", BytesWritten);
adl_atSendResponse ( ADL_AT_UNS, info);
}
wip_shutdown( pHttpClientCtx->DataChannel, FALSE, TRUE);
break;
In http_ClientTestCreate (original error now fixed in case someone does a cut/paste in the future for a HTTP POST example)
static s32 http_ClientTestCreate(void)
{
s32 ret = 0;
// HTTP Session creation
// ---------------------------
http_ClientTestCtx.CnxChannel = wip_HTTPClientCreateOpts(
NULL, // no handler
NULL, // no context
// default headers
// WIP_COPT_HTTP_VERSION, WIP_HTTP_VERSION_1_0,
WIP_COPT_HTTP_HEADER, "User-Agent", "WIPHTTP/1.0",
WIP_COPT_END);
if (http_ClientTestCtx.CnxChannel == NULL)
{
TRACE(( 1, "cannot create http session channel\n" ));
adl_atSendResponse ( ADL_AT_UNS, "cannot create http session channel\r\n" );
ret = -1;
}
else
{
// HTTP GET command
// ---------------------------
http_ClientTestCtx.DataChannel = wip_getFileOpts(
http_ClientTestCtx.CnxChannel, // session channel
HTTP_STR_URL, // requested URL
http_ClientTestDataHandler, // data handler
&http_ClientTestCtx, // context
WIP_COPT_HTTP_METHOD, WIP_HTTP_METHOD_POST,
// request headers
WIP_COPT_HTTP_HEADER, "Content-Type", "application/x-www-form-urlencoded",
WIP_COPT_HTTP_HEADER, "Content-Length", "20",
WIP_COPT_END);
if (http_ClientTestCtx.DataChannel == NULL)
{
TRACE(( 1, "cannot create http data channel\n" ));
adl_atSendResponse ( ADL_AT_UNS, "cannot create http data channel\r\n" );
wip_close( http_ClientTestCtx.CnxChannel);
ret =-1;
}
}
return(ret);
}
The PHP script is
<?php
function ReturnArrayKeysAndValues($pArray)
{
$ReturnString = "";
if (is_array($pArray))
{
reset($pArray);
while($ArrayCell = each($pArray))
{
$ReturnString .= "Key: " . $ArrayCell["key"] . "\tValue: " . $ArrayCell["value"] . "\r\n";
} // while
}
return($ReturnString);
}
$Body .= "\r\nPOST Array\r\n";
$Body .= ReturnArrayKeysAndValues($_POST);
$Body .= "\r\nGET Array\r\n";
$Body .= ReturnArrayKeysAndValues($_GET);
$Body .= "\r\nSERVER Array\r\n";
$Body .= ReturnArrayKeysAndValues($_SERVER);
print $Body;
?>
The HTML Page is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title></title> </head>
<body>
<FORM action="http://your URL/PostTest1.php" method="post">
<P>First name: <INPUT type="text" name="firstname"><BR> Last name: <INPUT type="text" name="lastname"><BR> <INPUT type="submit" value="Send"> <INPUT type="reset"> </P>
</FORM>
</body>
</html>