Logging API response body

Hi, I'm back once again - is there any way to log the parsed response body received from Discord? I can see the request body sent to Discord, but to improve my logging I want to be able to log the raw body. Looking at @discordjs/rest code, there doesn't seem to be any event emitted, and while a method was added to handle the parsing manually, it doesn't look like I can easily replace it without monkey-patching the REST package code. Am I missing something there? djs: 14.15.0-dev.1713658222-4ad285804
7 Replies
d.js toolkit
d.js toolkit8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
TomatoCake
TomatoCakeOP8mo ago
Which one would that be? I haven't been able to get that working as I want it (https://github.com/nodejs/undici/issues/1342)
GitHub
[diagnostics_channel] support body received? · Issue #1342 · nodejs...
This would solve... In a library I am working on, they had 2 events (request & response) that would fire before and after a request was made. The purpose of these events was for analytics (ie. ...
TomatoCake
TomatoCakeOP8mo ago
I've tried listening to the response REST event, while it returns a cloned Response I'm still unable to simply use response.json() (or <Response>.text(), ...) Unless someone has an idea on how to get that diagnostic channel to work, what would be the best way to request a feature like this? Should I just create a feature request on GitHub?
duck
duck8mo ago
the replies to this issue suggest using interceptors which look as though you could throw together an extremely hacky solution to achieve your goal but also I feel this is a relatively understandable feature request, even if it doesn't get implemented directly
TomatoCake
TomatoCakeOP8mo ago
Not sure whether you noticed, but I'm aware of that interceptors thing because that user talked to me. I've been using them since, but I apparently can only log the body which is sent, not the one received (without body-already-consumed errors)
const undici = require("undici")
const logBodyInterceptor = dispatch => {
return (opts, handler) => {
if (opts.body) console.log({
path: opts.method + " " + opts.path,
body: opts.body
})
return dispatch(opts, handler)
}
}

const client = new undici.Client("https://discord.com")
.compose(logBodyInterceptor)

const Discord = require("discord.js")
bot = new Discord.Client({
rest: {
agent: client
}
})
const undici = require("undici")
const logBodyInterceptor = dispatch => {
return (opts, handler) => {
if (opts.body) console.log({
path: opts.method + " " + opts.path,
body: opts.body
})
return dispatch(opts, handler)
}
}

const client = new undici.Client("https://discord.com")
.compose(logBodyInterceptor)

const Discord = require("discord.js")
bot = new Discord.Client({
rest: {
agent: client
}
})
(simplified)
duck
duck8mo ago
the extremely hacky solution I was referring to would just be overriding the onData handler to copy every chunk and reproduce the response body but also again, I feel this is a relatively understandable feature request
TomatoCake
TomatoCakeOP8mo ago
Made a feature request https://github.com/discordjs/discord.js/issues/10240 I'll take a look at onData, tho I recall trying onHeaders which didn't quite work
GitHub
Discord API REST response body access · Issue #10240 · discordjs/di...
Which application or package is this feature request for? rest Feature For debugging and logging purposes I want to be able to access the body the Discord REST API responded with after a request. W...
Want results from more Discord servers?
Add your server