using the new REST API in 2022.. multiple labels 追蹤

0
Avatar
Jeff Dickens

One request I've heard repeated in the past is the ability to print more than one label from a web services request.  Well now with 2022 you can !  There's probably more than one way, but here is one I have working in this test script:

const fetch = require('node-fetch');
const base64 = require('base-64');
const authUser = 'localbt';
const authPass = '<password here>';
const authString = 'Basic ' + base64.encode(authUser + ':' + authPass);
const btAPIBase = 'http://localhost:5159/api/actions';
const btAPIOptions = {
  Wait: '30s',
  MessageCount: '200',
  MessageSeverity: 'Info'
};
const btAPI = btAPIBase + '?' + Object.keys(btAPIOptions).map(key => key + '=' + btAPIOptions[key]).join('&');const btPrinter = 'wilkins-zebra2';
const btwFile = 'C:/label_files/gs1-128a.btw';
const actionScript = JSON.stringify({
  ActionGroup: {
      Actions: [
          {
              PrintBTWAction: {
                  Document: btwFile,
                  Printer: btPrinter,
                  SaveAfterPrint: false,
                  NamedDataSources: {
                      ship_addressee: 'ABC Company',
                      ship_address1: '1 Main St.'
                  }
              }
          },
          {
              PrintBTWAction: {
                  Document: btwFile,
                  Printer: btPrinter,
                  SaveAfterPrint: false,
                  NamedDataSources: {
                      ship_addressee: 'XYZ Company',
                      ship_address1: '1 High St.'
                  }
              }
          }
      ]
  }
}, null, 4);

console.log('sending', actionScript);
sendByFetch();
function sendByFetch() {
  url = btAPI,
  fetch(url,
      {
          method: 'POST',
          headers: {
              'Authorization': authString,
              'Content-Type': 'application/json'
          },
          body: actionScript,
          credentials: 'include'
      })
      .then(function (response) {
          var contentType = response.headers.get('content-type');
          if (contentType && contentType.includes('application/json')) {
              return response.json();
          }
          throw new TypeError('Oops, we haven\'t got JSON!');
      })
      .then(function (json) {
          console.log('response: ',json);
      })
      .catch(function (error) {
          console.log(error);
      });
}


I have a couple of questions, but to make this less messy I'll ask them as replies to this post, if I can do that.
 
 

1 意見

0
Avatar
Jeff Dickens
評論操作 永久連結

My first question is, what is the best practice for printing many labels with the same BTW file but different data on each ?  The fields on the label format are filled from named data sources.

My second question is how big can the ActionScript (in the POST body) be ?  Can it be 1MB ?

登入寫評論。