- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 03:46 AM
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.
/Petr
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 04:20 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 04:20 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 04:54 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 05:09 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 11:21 AM
So this is now solved,in my rest scripted api I used this method: saveResponseBodyAsAttachment()
sorry for confusing with converting to JSON.