1

I am getting a weird return with my response body it looks something like (in part):

�VJI��,K-�
                              �NM,Q�*)*M�Q�LQ��40�Q���K�K�MU�R
tT�IL�HM,)JU0�+�(�&��$U� T�����9�HMQH+��U004�P�UpJ,*�,��O)V02Q�QʃY�

I have been unable to decode it into a readable format. Here is part of my logic after removing my attempts to decode:

let emit = req.emit;
let body;
req.emit = function (eventName, response) {
            switch (eventName) {
                case "response": {
                    response.on("data", (d) => {
                        body += d;
                    });
            
                    response.on("end", () => {
                        console.log('Response: ', body);
                    });
                }
            }
            return emit.apply(this, arguments)
        }

How can I get the aggregated 'body' variable in string and/or JSON format?

Thanks in advance for any tips!

1 Answer 1

1

you need to readup more on encoding.

let emit = req.emit;
let body;
req.emit = function (eventName, response) {
            switch (eventName) {
                case "response": {
                    response.on("data", (d) => {
                        body += d;
                    });
            
                    response.on("end", () => {
                        const data = body.toString("utf-8")
                        console.log('Response: ', data);
                    });
                }
            }
            return emit.apply(this, arguments)
        }

more about encoding:convert streamed buffers to utf8-string

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I read up on it, and there was more helpful info there. Part of my issue, as it turns out, is sending a request with postman was causing a response body to still be not decoded, or it was malformed in some way. Postman must be doing something else to the response when I see it in a proxy server log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.