When I try to log the response body of my <https.p...
# suitescript
f
When I try to log the response body of my https.post, The log is just blank. It’s only when I add responseBody.code etc that I do get a value. But I need to see the response body to see its structure. What is a good way for this?
z
Coukd you please share the script? I am using a lot of https.post requests, and parse the responses
f
Copy code
const post = <http://https.post|https.post>({
            url: endpoint,
            body: JSON.stringify(json),
            headers: {
                'accept': 'application/json',
                'Authorization': '',
                'Content-Type': 'application/json',
            }
        })
        const responseBody = JSON.parse(post.body)
        log.debug('responseBody', responseBody)
        log.debug({ title: 'Client Response Code', details: post.code })
post.code gets me what I expect, but responseBody is just blank in the log
z
Usually, I test external endpoint's response with PostMan... You should know what is type of response, maybe it is required to set header of http request (Content-Type) If you expect JSON, try with JSON.parse(body)
f
I do parse the body?
const responseBody = JSON.parse(post.body)
And I know it’s json that I get
In some project I’ve set up a custom record to get a view of it. But feels very unnecessary
n
You see a 200 response? You could log the entire "post" object to see what that looks like, maybe the body is empty...
f
Yes, I do get a 200 response and if I log the body shouldn’t I get message, code etc? It not empty, when I’ve created a custom record and set the value of responseBody there I can clearly see everything. It just with the execution log that gives me nothing
n
Try : log.debug('response', post.body); Also, try the request in POSTman first and see if you get any response at all. I have worked with some applications that don't send a response on success.
f
Every object seems to just blank out
This is also blank
Copy code
const json = {
            invoice_id: invoiceId,
            type: 'creditnote',
            reference: rec.id,
            url: urlBuilder(record.Type.CREDIT_MEMO, rec.id),
            created_at: created
        }
        log.debug('json', json)
😕 1
z
@Felix Divall, your code is clean and correct. First, try what @NickSuite suggests to you, POSTMAN will help you to get a full response
f
Yes, postman will work. I know. But I would like the execution log to work as in some cases I need to generate accurate payloads that can be quite large
z
log.debug(‘json’,json) => log,debug(‘json’,JSON.stringify(MyJSON).substr(0,100)) it sounds stupid, but I will try without reserved words, and convert JSON object into string)
Finally, maybe there is a “ghost” … Did you try log.debug({ title: ‘Log title’, details: ‘Log details’}) … it is really strange, that everything is “blank”
f
Well, .substring gave me at least the text, so should be good enough
Just wish it was more structured
n
log.debug('response', 'This is a response debug log'); log.audit('response', 'This is a response audit log');
Try these first.
f
That kind of logging is not an issue, it’s only objects that fails me
z
I don't know the limit, but there is a limit for string length in log.debug statement
n
Wrap your code in try catch and log the catch as error. May be the script is crashing.
Because this
const responseBody = JSON.parse(post.body)
should be done like this:
if(post.body){
const responseBody = JSON.parse(post.body)
}
f
I always run with try/catch and the script is not crashing
z
if logdebug works with substring, try to get length : post.body.length just to check in length
👍 1
f
length is 167
a
your code looks fine, this is probably a chrome plugin going crazy, try disabling them