NetSuite HTTP POST Request to a Local IP Printer - netsuite

Ive been trying to send a http post request using a client script to send a zpl string to our local ip printer. I keep getting "The host you requested null is unknown or cannot be found."
What am I doing wrong? Do I need to use a RESTlet?
I can also do POST prints with this printer outside of netsuite, the problem is through netsuite.
function print(tag) {
var zpl = "^XA^CF0,30^FO20,20^FD" + tag.company + "^FS^FO20,50^FDPO # " + tag.poNum + "^FS^FO325,50^FDORD # " + tag.ordNum + "^FS^FO630,50^FDQTY^FS^CF0,50^FO700,40^FD" + tag.quantity + "^FS^BY2,10,100^FO" + calculateBarcodeDistance(tag.item.length) + ",175^BY3^BCN,50,Y,N,N,N^FD" + tag.item + "^FS^XZ";
var printUrl = "http://192.168.0.0/pstprnt";
var response = http.request({
method: http.Method.POST,
url: printUrl,
body: zpl
});
}

According to SuiteAnswers #44853 the N/http module is only supported in server side scripts:
So you would probably have to use a suitelet and call it from your client. Unfortunately, this would mean you have to expose a port on your public IP address and port-forward from there to the printer.
Alternatively, you could use the fetch API as suggested by Jon in comments, or an XMLHttpRequest, to achieve the same thing through the browser.

About the easiest thing to do would be to run ngrok on a box that has access to the internet. You’d post to the public address and that would forward to you printer. There’s a way to set a credential on that so you don’t get random print jobs from port scanners.
Then you’d use your current sample to send to a remote https address and that would send to ngrok on your server which would forward to your printer

You would need to create a simple Suitelet that uses the N/http module. So your client script would post to the printer suitelet, which would make the actual post and then return the data back to your client script.
You can use jQuery.post(...) to in your client script.

Another "easy" way to do this would be to install apache/nginx/other https capable server on the machine you are generating your labels on.
You'd map some useful name to one of the loop back addresses
You'd generate a self signed certificate for the host name above and have your server use that host and listen on the loop back address.
You'd configure the server as a reverse proxy to forward requests to the printer's IP address
You could then use send print requests to the local server (Use an iframe post hack) and though it's more work than the ngrok solution there's no direct cost associated.

I would say that in order to do this you need to POST to a server with a static IP address whereas your ip address in the example looks like an ip produced by a home router aka a dynamic ip.
The first line in the Netsuite help for the http module states that it can be used in client and server scripts. I have successfully sent a POST request via the http module to a http server and got a success response back. I did not have to use https.
You can test this by changing the url to http://httpbin.org:80/post, they provide a service that just sends whatever you post back to you.

Related

Socket.io failing to connect because of nsp

I have a problem with socket.io#^1.0. The setup is fine because it works locally, the server is correctly configured and when i try to connect to the server from my Angular APP it works fine with this:
io.connect("localhost:8080");
The connection is established and i can send and receive event. Now in the production environment, "locahost:8080" is replaced with the address of the server Launched:
io.connect("https://domain-name.com/api");
I know that the problem here is the /api, since socket.io is considering it as a namespace and it's trying to connect to it, in my network console I see 500 Internal server error with the address https://domain-name.com without the /api when i replace the request url to add the /api I get a 200 OK with type octet-stream.
So the question here is: how do I connect to the correct path without consideration of the namespace?
Thanks in advance for any help :)
I think you want to use the path option (documented here):
// client
var socket = io.connect('https://domain-name.com/', {
path : '/api/socket.io'
});

Node.js http GET request takes substantially longer than browser, REST client, et al

I am try to make a simple GET request in Node.js and the request takes 3-5 seconds to resolve whereas the same request in a browser or REST client takes ~400ms. The server to which I am making the request is controlled by our server team, but before I bother them with request/resource monitoring, I was going to ping the community to see if there were any "hey, check this setting first" kind of tips you guys could offer.
The code essentially forwards incoming requests to our server:
http.createServer(function (req, res) {
http.request({
host: "our.private.host",
port: 8080,
path: req.url,
headers: req.headers
}, function () {
res.end("DONE: " + Date.now());
}).end();
}).listen(8001);
I open my browser and type in the following URL:
http://localhost:8001/path/to/some/resource
... which gets forwarded on to the final destination:
http://our.private.host:8080/path/to/some/resource
Everything is working fine and I am getting the response I want, but it takes 3-5 seconds to resolve. If I paste the final destination URL directly in the browser or a REST client, it resolves quickly. I don't know much about our server, unfortunately - but I am looking more for node tips at this point. Note, the request pool isn't maxed out as I am only making 1 request at a time from my local machine.
The first step is gather some info on where the request is taking its time by looking at the exact timing of the network activity on your node server. You can do that by getting a tool that watches all network activity. I personally use Fiddler, but I know that WireShark is popular too.
Once that tools is installed and active, you can then see how long all these various steps in the process of your request are taking:
DNS request to resolve target IP address
Time to connect to the target server
Time to send the http request
Time to receive the http request
Time to send response back to original request
Understanding which of these operations is much longer than expected will give you an idea where to look further for the problem.
FYI, there are pre-built tools such as nginx that can do this type of proxying by just setting some values in a configuration file without any custom coding.

Meteor: How to get the hostname, server side

On the client I can use window.location.hostname to get the hostname. How can I get the same on the server?
I need this to work behind an Apache proxy, unfortunately Meteor.absoluteUrl() gives me localhost:3000. I also want it to work for different domains, I want one Meteor app that gives different results for different domains.
This question is somewhat related: Get hostname of current request in node.js Express
Meteor.absoluteUrl() given that your ROOT_URL env variable is set correctly.
See the following docs: http://docs.meteor.com/#meteor_absoluteurl.
Meteor doesn't know the outside-facing address of the proxy that it's sitting behind, and the (virtual) domain that this proxy was accessed by would have to be forwarded to the Meteor app for it to do what you are asking for. I don't think this is currently supported.
According to this you can now get the Host header inside Meteor.publish() and Meteor.methods() calls by accessing:
this.connection.httpHeaders.host
Elsewhere in the application, it's probably difficult to determine the Host header that is being used to connect.
If you want the hostname of the server, as configured in /etc/hostname for example:
With meteorite:
$ mrt add npm
In your server code:
os = Npm.require('os')
hostname = os.hostname()
This has no connection to the Host header provided in the incoming request.
updated answer with some of chmac's words from the comment below
In any server-side meteor file you can add:
if (Meteor.isServer) {
Meteor.onConnection(function(result){
var hostname = result.httpHeaders.referer; //This returns http://foo.example.com
});
}
You can fetch host as EnvironmentVariable from DDP object in method and publication. Meteor accounts-base package fetch userId via this way.
const currentDomain = function() {
const currentInvocation = DDP._CurrentMethodInvocation.get() || DDP._CurrentPublicationInvocation.get();
return currentInvocation.connection.httpHeaders.host;
}

How do I check connectivity with a machine on my local network using Node.js?

I am able to poll a website using Node.js but how do I check with some local machine.Just want to check if some "xyz" machine is active on the network.
I am not sure exactly what you wanna ask though
there is no difference between local servers and global servers.it's a only matter of whether you access to the server from inside of the LAN or outside of it.
if you install HTTP server on the machine you want to check, you can check its status code like usual website's servers.
and there is also modules for ping such as
https://npmjs.org/package/net-ping/
all you have to do is to replace target IP address to private IP address like 192.168..
Ok so I guess you used http to request an address?
If so, if you request a non active machine, you should get an error. To catch this error, simple react at the error event:
http.get("http://idontexistonthenetwork", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Documentation: http://nodejs.org/api/http.html#http_http_get_options_callback
P.S: Hope it answer your question. Don't hesitate to precise if you need something more specific like using websocket, IP discovering or else...

NodeJS, Express server with ssl to use Dropbox API - 400 Bad Request

I'm currently at y-hack, hacking up an app. I've never deployed an app to a server before, but I've managed to create an AWS EC2 instance, I created ca certificates with startssl, and now I'm trying to retrieve information using the DropBox API.
My code works on my local machine just fine, but I keep getting a 400 Bad Request Error when I try to use the code on my server. Here's what my options look like:
var options = {
key: fs.readFileSync('./cred/ssl.key'),
cert: fs.readFileSync('./cred/ssl.crt'),
ca: [fs.readFileSync('./cred/sub.class1.server.ca.pem')]
}
And my server looks like:
https.createServer(options,app).listen(443, function(){
console.log('Express server listening on port ' + 443);
});
When I try authenticating I use the built-in dropbox javascript client and call:
var server = new Dropbox.AuthDriver.NodeServer(500);
All my ports are open and I'm able to access my website with HTTPS. I've verified that my SSL certificate is okay, but every time I make a request from my micro instance to DropBox, the page hangs. I tried:
curl https://www.dropbox.com/1/oauth2/authorize?client_id={client_id}&redirect_uri=https%3A%2F%2Fsimplestever.com%3A8912%2Foauth_callback/&response_type=code/&state={state}
And I get this as a response (forgive the formatting):
Error (400)
It seems the app you were using submitted a bad request. If you would like to report this error to the app's developer, include the information below.
More details for developers
Missing "response_type".
=====================
I'm very new to this all and only taught myself today. I never used curl before... If anyone has any idea why I'm having these issues with the request, it would be incredibly helpful! Cheers!
Edit: I curled with the escaped characters and it worked! ...which means the client may be broken? I'll replace it with a query and forget about the csrf variable for now to see if it works.
Edit2: I ended up writing the authentication request using the request module and it worked! Just in the nick of time. Cheers!
Edit3: I should give credit to the code I imitated. https://github.com/smarx/othw/blob/master/Node.js/app.js
I think the issue with your curl command is that it has unescaped ampersands. Try putting quotes around the whole URL.

Resources