I'm trying to send POST data to a test.php file, which handles POST data only if it was provided a specific GET data. Unfortunately I have no clue how to do that; and have been searching for about an hour.
Any help would be appreciated, as i'm currently playing around a lot with this amazing stuff.
Thanks in advance;
Some clarification:
Say you have a index.php looking like this:
<?php
if (isset($_GET['p']))
echo count($_POST) . ' -- ' . count($_GET);
else echo 'fuuu';
?>
<form action="?p" method="POST">
<input type="submit" name="lolw" value="Go" />
</form>
If you submit that form, PHP's $_GET
and $_POST
superglobals will both contain 1 element.
Now, lets try to run that form through nodeJS.
Here is my test case (which is merely some cutpasting from the doku):
var http = require('http');
var options = {
hostname: 'localhost',
port: 80,
path: '/test.php?lolw=1&p',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
}).on('error', function (e) {
console.log('error in chunk');
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.toString());
});
// write data to request body
req.write('data\n');
req.end();
CLI output gives for the body: 0 -- 2 and then follows the form.
My point is: is it possible to send some parameters through GET and some others through POST, and specifying which needs to be sent through GET and which through POST ?
To make an HTTP request in node.js, you'll need to use http.request
. You can set the method to "POST" and still have get parameters in your URL. You'll need to set the Content-Length header to the length of your data and the Content-Type header to application/x-www-form-urlencoded
for a "standard" POST request.
Your question is pretty vague. If you're having problems with something in particular, just show your code and ask the more precise question.