Search code examples
node.jsnode-rest-client

Parsing node-rest-client response when the response is in HTML format


I have an endpoint I'm testing using Node.js node-rest-client

return new Promise(function(resolve, reject) {
    client.post(`${url}/auth/auth`, authArgs, function (authData) {
      if (debug) {
        console.log("Received authData:");
        console.log(authData);
        console.log();

if authData fails the response is in HTML, and when trying to print it I receive the following

Received authData:
<Buffer 49 6e 74 65 67 72 69 74 79 45 72 72 6f 72 20 61 74 20 2f 61 75 74 68 65 6e 74 69 63 61 74 69 6f 6e 2f 61 75 74 68 65 6e 74 69 63 61 74 65 0a 6e 75 6c ... >

How can I have this HTML to be parsed properly so it can show the error message properly instead of a buffer


Solution

  • The output error is Buffer, its a stream of binary, and to convert it to string you can just do:

    ...
    console.log(authData.toString('utf8'));
    ...