The CreatorCon Call for Content is officially open! Get started here.

REST API - inbound attachment - need help - application/octet-stream - issue/challenge

Petr Pastuszek1
Tera Contributor

Hello Community,

My use case:

 

3rd party tool send me to ServiceNow object towards predefined REST API end point

I will run POST message towards 3rd party system to get access_token --> All OK

When I have access token, I run another GET call towards 3rd party to get attachment data --> all OK

 

However response arrived in application/octet-stream (it have to arrive like this, supplier requirement) content, see below and here Im stucked as I don't know how to read it or convert it to JSON object so I could read for example properties such as fileName, size....base64 string ....

Please advise if this was already solved by someone.

 

Img of response Im not able to work with current knowledge.

 

PetrPastuszek1_0-1707997589956.png

 

 

/Petr

1 ACCEPTED SOLUTION

Prashant Ahire
Kilo Sage

Hello @Petr Pastuszek1 

Dealing with an application/octet-stream response containing JSON data can be tricky, but it’s definitely solvable. Here’s how you can handle it in ServiceNow:

Blob to JSON Conversion:

  • When the response arrives as an application/octet-stream, you’ll need to convert it to a readable format.
  • Use the following approach to decode JSON messages from the blob:
// Assuming you have the response object
try {
    // If the response is successful
    const blob = new Blob([response.data]); // 'response.data' contains the blob
    const data = await blob.text();
    const { fileName, size, base64String } = JSON.parse(data);
    // Now you can access properties like fileName, size, and base64String
} catch (error) {
    // Handle any errors here
}

 

Please check and Mark Correct and Helpful if it really helps you.
Regards,
Prashant Ahire

View solution in original post

4 REPLIES 4

Prashant Ahire
Kilo Sage

Hello @Petr Pastuszek1 

Dealing with an application/octet-stream response containing JSON data can be tricky, but it’s definitely solvable. Here’s how you can handle it in ServiceNow:

Blob to JSON Conversion:

  • When the response arrives as an application/octet-stream, you’ll need to convert it to a readable format.
  • Use the following approach to decode JSON messages from the blob:
// Assuming you have the response object
try {
    // If the response is successful
    const blob = new Blob([response.data]); // 'response.data' contains the blob
    const data = await blob.text();
    const { fileName, size, base64String } = JSON.parse(data);
    // Now you can access properties like fileName, size, and base64String
} catch (error) {
    // Handle any errors here
}

 

Please check and Mark Correct and Helpful if it really helps you.
Regards,
Prashant Ahire

Thank you for your quick response.

Not working, there is a syntax problem, it is missing ';' on row  below

 

const data = await blob.text();

 

Im trying execute in background script.

Maybe one more question.

first Im provided with below object, I take some values of it and run GET to get attachment itself, all good so far.

 

When I get response (this is my problem on last image below), I need to create attachment out of that response, this is my main problem. Sorry if I confused with requirement.

{
  "id": "string",
  "companyId": "string",
  "cfcTicketId": "string",
  "customerRefId": "string",
  "category": "SUPPORT_REQUEST",
  "createdBy": "string",
  "createdOn": "2024-02-14T09:23:59.494Z",
  "fileSize": 0,
  "fileName": "string"
}

/Petr

PetrPastuszek1_0-1708002595287.png

 

 

So this is now solved,in my rest scripted api I used this method: saveResponseBodyAsAttachment()

sorry for confusing with converting to JSON.