Error 500 when trying to send file to printer via API

// Expert user has replied.
T Tim Schelpe 3 years 1 month ago
641 4 0

Hi
I'm trying to send files to the printer from Microsoft Business central (AL coding language).
My request contains the apikey and tenant headers and my body looks like this:

--ac75c5016-d2f8-41a2-a2d5-494b247babcbf
Content-Disposition: form-data; name="sn"

D6J211603681
--ac75c5016-d2f8-41a2-a2d5-494b247babcbf
Content-Disposition: form-data; name="zpl_file"; filename="label.txt"
Content-Type: text/plain

^XA ^FO50,50^ADN,36,20^FDHello World^FS ^XZ
--ac75c5016-d2f8-41a2-a2d5-494b247babcbf--

I get following error in return:
{"fault":{"faultstring":"Execution of JS-ExtractPrinterList failed with error: Javascript runtime error:
"TypeError: Cannot read property
"1
" from null. (JS-ExtractPrinterList.js:7)
"","detail":{"errorcode":"steps.javascript.ScriptExecutionFailed"}}}
which is a server-side (500) error.

Can anyone tell me if I'm missing something or did something wrong?
Thanks in advance for any help

Please Register or Login to post a reply

4 Replies

S Steven Si

First, make sure you are able to print through the Curl command with your apikey, tenant ID, printer SN and the file. The Curl format is shown below. This is to ensure that we have everything set up properly. You can also verify your settings with the JavaScript demo code, which can be downloaded from GitHub - Print From the Cloud Demo. Then we can focus on the AL code.

curl -v -H "apikey: yourApikey" \
     -H "tenant: yourTenantID" \
     -F "sn=XXRBJ143300207" \
     -F "zpl_file=@HelloWorld.txt" \
     https://stage-api.zebra.com/v2/devices/printers/send

By just looking at the multipart form-data body, I don't see any wrong. But I definitely would look into the AL code to see if any extra headers being added before it sends the request out.

T Tim Schelpe

I was able to send files to the printer form postman, which does the same as curl as far as I'm aware.
The headers in AL are added like this: (apikey and tenant left empty for obvious reasons)

vHttpContent.GetHeaders(vHttpHeaders);
vHttpHeaders.Add('apikey', '');
vHttpHeaders.Add('tenant', '');
vHttpHeaders.Remove('Content-Type');
vBoundary := generateBoundary();
vHttpHeaders.Add('Content-Type', 'multipart/form-data; boundary=--' + vBoundary);

the request itself is configured like this:
vHttpRequest.Method('POST');
vHttpRequest.SetRequestUri('https://api.zebra.com/v2/devices/printers/send');

and I send the request like this:
vHttpClient.Post('https://api.zebra.com/v2/devices/printers/send', vHttpContent, vHttpResponse);

The response then contains the error with status code 500

When debugging the code, the debugger only shows the headers I manually added. Debugging APIs is kind of a mess in AL.

Thank you for your help and let me know if I need to provide more information.

S Steven Si

I am unfamiliar with AL language. The 500 error indicates a server internal error, which means that the server cannot process the request. In most cases, the 500 error is caused by the request body. Is there a way to output the request body from the code above, so that we can visually check if anything is missing or extra in the multipart form-data?

T Tim Schelpe

--ac75c5016-d2f8-41a2-a2d5-494b247babcbf
Content-Disposition: form-data; name="sn"

D6J211603681
--ac75c5016-d2f8-41a2-a2d5-494b247babcbf
Content-Disposition: form-data; name="zpl_file"; filename="label.txt"
Content-Type: text/plain

^XA ^FO50,50^ADN,36,20^FDHello World^FS ^XZ
--ac75c5016-d2f8-41a2-a2d5-494b247babcbf--

this is an exact output of the body

CONTACT
Can’t find what you’re looking for?