1

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
  });
}
Nick
  • 517
  • 2
  • 13
  • 1
    Are you able to make a request to the printer using the browsers native http request module fetch? Like this `fetch(printUrl, { method: 'post', body: zpl }).then(response => response.json()).then(data => console.log('Data', data));` This should help determine if this is a NetSuite issue or if it's an issue with your printer server. – Jon Lamb Jul 27 '18 at 16:37

5 Answers5

2

According to SuiteAnswers #44853 the N/http module is only supported in server side scripts:

enter image description here

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.

Krypton
  • 4,394
  • 2
  • 9
  • 13
  • So the problem with an XMLHttpRequest is that netsuite's pages are HTTPS so I cant make browser HTTP requests or else Ill get a "Mixed Content: The page at '***' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint" error. And the printer only accepts HTTP. Anyone have an idea for a workaround? – Nick Jul 27 '18 at 18:01
1

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

bknights
  • 14,408
  • 2
  • 18
  • 31
0

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.

dcrs
  • 294
  • 1
  • 4
0

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.

bknights
  • 14,408
  • 2
  • 18
  • 31
0

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.

Superdooperhero
  • 7,584
  • 19
  • 83
  • 138