AmericanKryptonite
AmericanKryptonite

Reputation: 147

Using cURL to POST to basic form

I thought this would be simple but I can't figure it out. I have a form with this structure:

<form name="input" action="DOTHINGS" method="POST">

<textarea rows="10" cols="30" name="rules"></textarea>

<textarea rows="10" cols="30" name="facts"></textarea>

<br><input type="submit" value="Submit">

So I want to post to the rules and facts text areas and submit. I have tried the following:

curl -F "rules=@/directory/file.txt" -F "facts=@/directory/file.txt" http://localhost:1112/
curl -X POST -d "rules=junktext" http://localhost:1112/
curl --data "rules=junkdata&facts=junkdata" http://localhost:1112/

No matter what I try I get The requested resource could not be found as a response.

I first thought my localhost:1112 was not accessible so I tried variations of that and did a basic curl localhost:1112 which returned the content of the site so it seems it's able to access that URL. However I just can't get anything to POST. Is there something wrong with my syntax or with the form itself?

Upvotes: 0

Views: 2197

Answers (3)

Rocky Pulley
Rocky Pulley

Reputation: 23321

I believe something like this should work:

curl -X POST -d "rules=junktext" -H "Content-Type: application/x-www-form-urlencoded" http://localhost:1112/DOTHINGS

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247210

Since my comment was the answer:

The action attribute of an HTML form usually names a (relative) URL. http://www.w3.org/TR/html401/interact/forms.html#edef-FORM

In this case, you need to curl the URL of the form handler:

curl -d "..." http://localhost:1112/DOTHINGS

Upvotes: 1

TheSatinKnight
TheSatinKnight

Reputation: 744

You could verify with your apache logs what is going on. Often this results in at least knowing if the data went where you expected it to.

You could do this in php and probably get a little more feedback. Especially since the php CLI configuration is very vociferous about notices and errors. Still execute from the command line by putting it in a php file and executing the php file with the php binary instead of curl directly. IF you have php installed, of course. Leave the proxy stuff blank '' if you don't know what it is.

PHP example:

<?PHP
$data='x_response_code=1&x_response_reason_code=1&x_response_reason_text=This+transaction+has+been+approved.&x_ship_to_city=auburn&x_ship_to_state=New+York&x_ship_to_zip=13021&x_ship_to_country=United+States&x_amount=2.00&x_tax=0.00&x_duty=0.00&x_freight=0.00&x_tax_exempt=FALSE&x_po_num=&x_MD5_Hash=AF489908B0730A34FF3DF9&x_cvv2_resp_code=M&x_cavv_response=&x_test_request=false';

$response=curl_post_data_and_return('http://catalog.example.com/capture/CaptureAllFormData.php',$data,'','off');

echo "<pre>";
print_r($response);

function curl_post_data_and_return($site,$data,$proxy,$proxystatus){
    $ch = curl_init();
    if ($proxystatus == 'on') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_URL, $site);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//    curl_setopt($ch, CURLOPT_COOKIEFILE, "xxx.cookie.txt");
//    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
//    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//    curl_setopt($ch, CURLOPT_REFERER, 'http://www.example.com//search.asp');
    ob_start();      // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($ch);
}

Upvotes: 0

Related Questions