I get the PDF using a get request, how to I conver...
# ask-stanley-ai
a
I get the PDF using a get request, how to I convert the body of the response to a PDF object
c
AI generated answer (using internal Slack threads):
Based on the conversations, it seems that the issue is related to the handling of binary content in NetSuite. When you make a GET request to an API that returns a PDF file, the response body is treated as a UTF-8 string by NetSuite. This can lead to encoding errors and the PDF file being corrupted. To solve this issue, you need to ensure that the Content-Type header of the response is recognized by NetSuite as binary. Ideally, this should be
application/pdf
. If the Content-Type header is correctly set, the response body should be in base64 format, which you can directly save into the file cabinet. Here is a sample code snippet:
Copy code
javascript
let result = https.get({
    url: url+'/api/v1/wps/'+row.wp_id+'/log-exports/'+row.pdf_id+'/download',
    headers: {
        Authorization: 'Bearer '+token_id,
        Host: url,
        Accept: '*/*'
    }
});

let filePdf = file.create({
    name: logPdf.fileName,
    fileType: file.Type.PDF,
    contents: result.body,
    folder: 4127544
});
In this code,
result.body
is the base64 encoded content of the PDF file. This is directly used as the contents of the file being created. If the server you are working with can't set the Content-Type header to exactly
application/pdf
, you might need to look for alternatives. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.840017557
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.831069231
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.820939302
Please provide your feedback on this answer.