I am trying to call this api using nodejs - https://developer.zebra.com/apis/sendfiletoprinter-model#/SendFileToPrinter/SendFiletoPrinter
It accepts a zpl_file as a string(binary) it says in the documentation.
I have a locally stored zpl file and want to attach it to my post request using NodeJs code.
This is the code I have so far, but I can't get it to work. Any help on this would be highly appreciated. Thanks!
exports.PrintUsingExampleCode = async function(payload) {
let url = "https://api.zebra.com/v2/devices/printers/send";
let apikey = "<apikey>";
let tenant = "<tenant>";
let sn = "<printerSN>";
let filepath = 'C:\\ZPL\\HelloWorld.zpl'
var req = require('request');
var fs = require('fs');
var fileToSend = fs.readFile('C:\\ZPL\\HelloWorld.txt');
req.post({
url: url,
form: { sn: sn, tenant: tenant, apikey: apikey, zpl_file : fileToSend
},
headers: {
'sn': sn,
'apikey' : apikey,
'tenant' : tenant
},
method: 'POST'
},
function (e, r, body) {
console.log(body);
});
};
pass the file as a readable stream and serial number in the form part, and apiKey and tenant as headers:
let url = "https://api.zebra.com/v2/devices/printers/send";
let apikey = "<apikey>";
let tenant = "<tenant>";
let sn = "<printerSN>";
let filepath = 'C:\\ZPL\\HelloWorld.zpl'
var req = require('request');
var fs = require('fs');
var fileToSend = fs.createReadStream(filepath);
req.post({
url: url,
formData: {
sn: sn,
zpl_file: fileToSend
},
headers: {
'apikey': apikey,
'tenant': tenant
},
method: 'POST'
},
function(e, r, body) {
console.log(body);
});