unable to send parameters using http.request in node.js - node.js

The script that I am using is:
var http = require('http');
var options = {
host: 'some url',
port: 80,
path: 'path',
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);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('username=abcd&password=defg');
req.end();
I am getting a response as 404 bad request by server.. but when I use the firefox add-on "poster" for posting the same parameters to the server.. I am getting a proper reply, i.e 200 status code.. I think this the problem with the string I am passing in req.write(). but the same string is working fine with poster.. Can someone explain what the issue is?

It looks to me that a Content-Type header is missing. It probably should be set to application/x-www-form-urlencoded

Related

cURL and node give the same error, but can't fix node the same way

My code seems pretty solid but it returns no body and status code 500. Interestingly, when I try to do it with cURl the same thing would happen, until I changed the http to https, then it worked perfectly. I feel like the same thing is happening in node, except its already https?
var https = require('https');
var options = {
host: "*******", // IP Address
port: *****,
path: '/***/***/***',
method: 'POST',
auth: '***:***'
};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
https.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);
});
}).end();
The Error is:
STATUS: 500
HEADERS: {"content-length":"61","content-type":"application/json","server":"Microsoft-HTTPAPI/2.0","date":"Thu, 08 Sep 2016 10:23:48 GMT","connection":"close"}
BODY: "The body of the message cannot be read because it is empty."

Convert Curl Request Over to Node.JS Server -- Attach data to the request?

I'm learning how to process credit card payments.. Here is the test CURL...
curl -k -v -X POST -H "Content-Type:application/json" -H "Authorization: Basic Mxxxxxxxxxxxxxxxxxx=" -d "#json_file.txt" -o output.txt https://w1.xxxxxxxxxxxx.net/PaymentsAPI/Credit/Sale
Where json_file.txt contains
{
"InvoiceNo":"1",
"RefNo":"1",
"Memo":"TEST_TEST_PHONY",
"Purchase":"1.00",
"AccountSource":"Swiped",
"AcctNo":"5xxxxxxxxxxxxxxxxx1",
"ExpDate":"0816",
"OperatorID":"xxxxxxxxxx",
}
I converted over to node module HTTPS
var https = require("https");
var options = {
host: 'w1.xxxxxxxxxxxxxx.net',
port: 443,
path: '/PaymentsAPI/Credit/Sale',
headers: { "Content-Type" :"application/json",
"Authorization" : "Basic Mxxxxxxxxxxxxxxxxxxxxxxxxxxx="} ,
data: {
"InvoiceNo":"1",
"RefNo":"1",
"Memo":"xxxxxxxxxxxxxxx",
"Purchase":"1.00",
"AccountSource":"Swiped",
"AcctNo":"5xxxxxxxxxxxxxxxxx1",
"ExpDate":"0816",
"OperatorID":"xxxxxxxxxxxx",
},
method: 'POST'
};
// oops... 400 Bad Request
// The request could not be understood by the server due to malformed syntax.
var req = https.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.toString() );
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
Problem is on the original curl request, the data contained within the JSON text file is submitted as a POST attachment. Curl request works fine. On the other hand its not clear to me on how to do that from a node.js server. The response headers come back fine, but I get a 400 response (malformed syntax). Anybody know how to attach JSON data as a post attachment to a HTTPS request?
Oops. I totally didn't understand how the req.write() stuff works.. This code is successful. Thanks to Dan Ourada # Mercury Payments for his assistance. Note, all code here is pure sandbox. No real $$ going buy buy.
var https = require("https");
var options = {
host: 'w1.mercurycert.net',
port: '443',
path: '/PaymentsAPI/Credit/Sale',
headers: { "Content-Type" :"application/json", "Authorization" : "Basic MDAzNTAzOTAyOTEzMTA1Onh5eg=="},
method: 'POST'
};
var inputdata = JSON.stringify( {
"InvoiceNo":"1",
"RefNo":"1",
"Memo":"XXXXX",
"Purchase":"1.00",
"AccountSource":"Swiped",
"AcctNo":"5499990123456781",
"ExpDate":"0816",
"OperatorID":"money2020",
} );
var req = https.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('Return info: ' + chunk); // output the return raw data
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// attach input data to request body
req.write(inputdata);
req.end();
Info offered here in case anybody else gets stuck converting from a CURL command over to a Node.js http request...
And hey, after playing around with this, I'm amazed on how truly easy it is to incorporate a real (and secure) payments system into any merchant website. (Obviously Https site required.)

Error using http.request in node.js

This is my little piece of code:
var options = {
host: 'www.cuantocabron.com',
port: 80,
path: '/index.php',
method: 'GET'
};
var http = require('http');
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);
});
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
I'm trying to get all the HTML code from that URL; the firts time that I run that code, the console prints all the HTML, but after that, throw an error, so my server goes down.
But, when I run it, the console throw an Error:
events.js:71
throw arguments[i]; //Unhandled 'error' event
Error: Parse Error
at Socket.socketOnData (http.js:1447:20)
at TCP.onread (net.js:404:27)
Any solution?
Thanks;
If you are only trying to get the HTML from that URL, you do not need the req.write statements. Those are the statements that are causing your error. You use req.write when you are writing data to the server, E.G. If you were doing a POST.
You should also add an on error handler after you declare your req
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

Node.js and protocol buffers - How to parse a PB from a post

I load by PB schema as follows:
var Schema = require('protobuf').Schema;
var schema = new Schema(fs.readFileSync('/home/ubuntu/workspace/test.desc'));
Then for a post I expect a pb, I have the following.
app.post('/mypost', function(req, res){
var Feed = schema['MyRequest'];
var aFeed = Feed.parse(req.body);
var serialized = Feed.serialize(aFeed);
});
I am rather new to node.js and also getting post data. is the req.body the buffer from the post data?
TypeError: Argument should be a buffer
at Function.parse (unknown source)
at /home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/bidder.js:71:22
at callbacks (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:272:11)
at param (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:246:11)
at pass (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:280:4)
at Object.handle (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:45:10)
at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js:204:15)
at Object.methodOverride [as handle] (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js:204:15)
Looking for specific examples for simple request response handling in http with nodejs. This should help you get to the parsing step.
http://nodejs.org/api/all.html#all_class_http_serverrequest
http://nodejs.org/api/http.html#http_event_data
example:
var options = {
host: 'www.google.com',
port: 80,
path: '/upload',
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);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
example see explanation of request and response in this answer
https://stackoverflow.com/a/4696338/51700
another example with cookies
https://stackoverflow.com/a/4581610/51700

Pull a specific string from an HTTP request in node.js

NOOb here. I've got a HTTP request that pulls all of the content from a specific webpage. However, all I need is a specific string:"Most recent instantaneous value: ". In fact, I actually need to store the value that follows value:. Here is my code:
var http = require("http");
var options = {
host: 'waterdata.usgs.gov',
port: 80,
path: '/ga/nwis/uv?cb_72036=on&cb_00062=on&format=gif_default&period=1&site_no=02334400',
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);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
I realize I don't need all the console.log statements, but do I need keep console.log('BODY: ' + chunk); so all of the data downloads?
Never do it the way I'm doing it in this quick'n'dirty example. There are plenty of modules for DOM traversal, HTML/XML parsing, etc... They are a lot safer then a simple regex. But just so you get the general idea:
var http = require("http");
var options = {
host: 'waterdata.usgs.gov',
port: 80,
path: '/ga/nwis/uv?cb_72036=on&cb_00062=on&format=gif_default&period=1&site_no=02334400',
};
function extract (body, cb) {
if(!body)
return;
var matches=body.match(/Most recent instantaneous value: ([^ ]+) /);
if(matches)
cb(matches[1]);
}
http.get(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
extract(chunk, function(v){ console.log(v); });
});
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
});
Somehow I also got a different page when sending a POST instead of a GET request. So I changed that bit...
Regarding your second question: No you don't need to keep any of the console.log() statements. Just use callbacks and everything is fine! :-)

Resources