I have tried a bit of code to upload a file on node based server and i'm getting a socket hang up error.
After googling about the error I came across a post which suggested that request.end() if not called causes this error but as you will see code below I do have called request.end().
Any help,suggestion are appreciated
var http = require('http');
var fs = require('fs');
var options = {
hostname: 'api.built.io',
port : 443,
path : '/vi/uploads',
method : 'POST'
};
var request = 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);
});
});
var size;
fs.stat('/home/abhijeet/Downloads/fgo-2.jpg',function(err,stats){
size = stats.size;
console.log(size);
});
var boundaryKey = Math.random().toString(16); // random string
request.setHeader('Content-Type', 'multipart/form-data; boundary="'+boundaryKey+'"');
request.setHeader('application_api_key','1234');
request.setHeader('authtoken','123');
request.setHeader('Content-Length','42215');
// the header for the one and only part (need to use CRLF here)
request.write(
'--' + boundaryKey + '\r\n'
// use your file's mime type here, if known
+ 'Content-Type: image/jpeg\r\n'
// "name" is the name of the form field
// "filename" is the name of the original file
+ 'Content-Disposition: form-data; name="upload[upload]"; filename="/home/abhijeet/Downloads/fgo-2.jpg"\r\n'
+ 'Content-Transfer-Encoding: binary\r\n\r\n'
);
var readFile = fs.createReadStream('/home/abhijeet/Downloads/fgo-2.jpg', { bufferSize: 4 * 1024 })
.on('end', function() {
request.end('\r\n--' + boundaryKey + '--'); // mark the end of the one and only part
})
.pipe(request, { end: false }) // set "end" to false in the options so .end() isn't called on the request
request.on('error',function(error){
console.log(error);
});
// maybe write directly to the socket here?
request.end();
// console.log(readFile);
Related
I have manage to get an API working for a product I have started to use, I can successfully run the below code and update a record in the api's database (I have removed all the api's soap xml code to make it look cleaner), I am trying to save the output as a variable so i can then process it in php,
I am a beginner to Javascript but I cant find much help on saving the output.
If someone could point me in the right direction I would be forever grateful,
I just need to console.log output in a variable rather than in the console.
var https = require("https");
var xml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
'<soap:Header>' +
'</soap:Header>' +
'<soap:Body>' +
'</soap:Body>' +
'</soap:Envelope>';
var username = "";
var password = "";
var options = {
host: "",
port: 443,
method: "POST",
path: "",
// authentication headers
headers: {
'Content-Type': "text/xml; charset=utf-8",
'Content-Length': Buffer.byteLength(xml),
'Authorization': "Basic " + new Buffer(username + ":" + password).toString("base64"),
'SOAPAction': "",
'Accept': "application/json"
}
};
//The call
request = https.request(options, function (res) {
console.log("statusCode:", res.statusCode);
res.on("data", (d) => {
process.stdout.write(d);
});
});
request.on("error", (e) => {
console.error(e);
});
request.end(xml);
If you want to save the output, then you need to save the data from the variable that contains the output (which you have called d).
console.log("statusCode:", res.statusCode);
var data = "";
res.on("data", (d) => {
data += d;
});
res.on("end", x => {
// data is now ready
});
Note that you will probably run into the issue described in this question and you would probably be better off using an HTTP client library that natively supported promises, such as Axios.
All,
I am trying to figure out how to pass the results from an https.request in node.js code out to a variable. I have an https.request setup that correctly passes the correct information to a SOAP API and gets the correct response back. My ultimate goal is to get the output from the https.request into a variable that I can call using Express.
Here is are my code chunks.
HTML:
<div class="row">
<div class="col-md-12" class="pull-left">
<p> TEST </p>
<p>{{soapreply}}</p>
</div>
JS:
app.post('/cucmmapper/submit', function (req, res) {
// FORM - DATA COLLECTION
var cucmpub = req.body.cucmpub;
var cucmversion = req.body.cucmversion;
var username = req.body.username;
var password = req.body.password;
var authentication = username + ":" + password;
var soapreplyx = '';
// SOAP - BUILD CALL
var https = require("https");
var headers = {
'SoapAction': 'CUCM:DB ver=' + cucmversion + ' listCss',
'Authorization': 'Basic ' + new Buffer(authentication).toString('base64'),
'Content-Type': 'text/xml; charset=utf-8'
};
// SOAP - AXL CALL
var soapBody = new Buffer('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
'<ns:listCss sequence="?">' +
'<searchCriteria>' +
'<name>%</name>' +
'</searchCriteria>' +
'<returnedTags uuid="?">' +
'<name>?</name>' +
'<description>?</description>' +
'<clause>?</clause>' +
'</returnedTags>' +
'</ns:listCss>' +
'</soapenv:Body>' +
'</soapenv:Envelope>');
// SOAP - OPTIONS
var options = {
host: cucmpub, // IP ADDRESS OF CUCM PUBLISHER
port: 8443, // DEFAULT CISCO SSL PORT
path: '/axl/', // AXL URL
method: 'POST', // AXL REQUIREMENT OF POST
headers: headers, // HEADER VAR
rejectUnauthorized: false // REQUIRED TO ACCEPT SELF-SIGNED CERTS
};
// SOAP - Doesn't seem to need this line, but it might be useful anyway for pooling?
options.agent = new https.Agent(options);
// SOAP - OPEN SESSION
var req = https.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (d) {
soapreplyx = d;
console.log("Got Data: " + d);
});
});
// SOAP - SEND AXL CALL
req.write(soapBody);
res.render('cucmmapper-results.html'), {
'title': 'CUCM 2.1',
'soapreply': soapreplyx
};
req.end();
req.on('error', function (e) {
console.error(e);
});
});
}
The line "console.log("Got Data: " + d)" is getting the correct expected reply from the API, however, I can't figure out how to get that data into my variable "soapreplyx" which changes in Express to "soapreply".
Much appreciated for any help you might have!
You're not waiting for your request to respond before you call res.render(), so the value of soapreplyx is always '', its initial value. To correct this, add an 'end' event listener on the response object passed to your https.request() callback.
You're not appending the chunks of the response to your soapreplyx variable, you're reassigning its value with each successive chunk.
let soapRequest = https.request(options, soapResponse => {
soapResponse.on('data', chunk => {
soapreplyx += chunk
})
soapResponse.on('end', () => {
return res.render('cucmmapper-results.html', {
title: 'CUCM 2.1',
soapreply: soapreplyx
})
})
})
soapRequest.write(soapBody)
soapRequest.end()
I was wondering if we can measure the time it takes for an http request to be completed using node.js. Modifying slightly an example from the documentation (here), one can easily write down the following code.
var http = require('http');
var stamp1 = new Date();
var stamp2, stamp3, stamp4;
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
stamp3 = new Date();
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
res.on('end', function () {
stamp4 = new Date();
console.log ("Stamp 3: " + stamp3);
console.log ("Stamp 4: " + stamp4);
});
});
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();
stamp2 = new Date();
console.log ("Stamp 1: " + stamp1);
console.log ("Stamp 2: " + stamp2);
Now let me come to my point. On the response one can easily measure the time it takes for the response, since in the beginning stamp3 is set and on end stamp 4 is set. So, in principle for relatively large amounts of data these two timestamps will be different.
However, the question that I have is whether stamps 1 and 2 actually measure what is happening when the request is being prepared and dispatched. In other words, is req.write(....) a synchronous operation? Based on node.js principles I would expect req.write(...) to be an asynchronous operation where one can pass an arbitrarily large document and then upon successful completion we can have a callback knowing that the request has finished.
Comments?
Two function already exist for that:
console.time(id), start the timer
console.timeEnd(id) end the timer, print id followed by the time in ms
So in your case:
var req = http.request(options, function(res) {
console.time('Requete: '); //Begin to count the time
stamp3 = new Date();
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
res.on('end', function () {
stamp4 = new Date();
console.log ("Stamp 3: " + stamp3);
console.log ("Stamp 4: " + stamp4);
console.timeEnd('Requete: '); //Will print "Requete: X" with X being the time in ms
});
});
The docs mention no callback, so I'm assuming req.write as well as res.end to be synchronous.
So in your case if you're referring to just that one request that you're initializing, I think the time measurement should be accurate. I don't expect the time difference to be very big though (perhaps even in the same milisecond).
I have to do retrieve the page source of a page with nodejs, but the page that I want to retrieve isn't always the same.
I have 2 files server.js that is listening and when he receive A connections he call load.js that retrive the sourse of a non defined page, my code is this:
server.js
var net = require('net');
var loadFb = require('./load.js');
var HOST = 'localhost';
var PORT = 9051;
// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {
// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {
console.log('User request profile of: ' + data);
// Write the data back to the socket, the client will receive it as data from the server
//here I have to call test.js
//how
sock.write(data);
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);
the other file is this:
var https = require('https');
var options = {
host: 'graph.facebook.com',
port: 443,
path: '/dario.vettore',
method: 'GET'
};
var req = https.get(options, function(res) {
var pageData = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
pageData += chunk;
//console.log(pageData);
return pageData;
});
res.on('end', function(){
//response.send(pageData)
});
});
How can I ask from the first file (server.js) to the second file to retrive for it the page source from the second file, But the page that I want to get the source can change isn't always the same..
In your second file (I'm assuming that's the one named loadFb.js), you want to export a function, instead of calling the code right away.
Node caches its modules so when you require() them, the code only gets run once.
The second file should look something like this:
var https = require('https');
module.exports = function(path, callback) {
var options = {
host: 'graph.facebook.com',
port: 443,
path: path,
method: 'GET'
};
var req = https.get(options, function(res) {
var pageData = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
pageData += chunk;
});
res.on('end', function(){
callback(pageData);
});
});
};
Then in your first file, you would access it like this:
loadJs('/dario.vettore', function(pageData) {
console.log(pageData);
});
This way you can execute the module code many times, with different paths.
I have the following code:
var options1 = {
host: 'maps.googleapis.com',
port: 80,
path: "/maps/api/geocode/json?latlng=" + lat + "," + lng + "&sensor=false",
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
var body1 = "";
var req = http.request(options1, 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);
body1 += chunk;
});
res.on('close', function () {
console.log('get_zillow : ' + body1);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
console.log('get_zillow : ' + body1);
I need to populate body1 with the result of the JSON response. However, the first console.log('get_zillow : ' + body1); never gets called -- for some reason the result never closes -- and the second console.log('get_zillow : ' + body1); prints nothing, since it is asynchronous and gets called before body1 gets populated.
Additionally, I need to make similar requests to different external sites several times in succession, with each request dependent on the json from the previous result. Is there any way to do this without writing three messy inner callbacks, and somehow block after an http request?
Change
res.on('close', function () {
console.log('get_zillow : ' + body1);
});
to
res.on('end', function () {
callback_function(body1);
});
//defined new function
function callback_function(finaldata)
{
// handle your final data
}