aacerox / node-rest-client

REST API client from node.js
MIT License
377 stars 132 forks source link

XML parse results in Buffer, not JS object #133

Closed htor closed 7 years ago

htor commented 7 years ago

I'm having troubles getting automatic parse to work with XML responses. Using version ^2.0.1. The JSON responses from the server are parsed successfully into an object, but the XML responses becomes a Buffer for some reason.

client.get('http://localhost:8080/xml', function (data, response) {
    console.log('GOT DATA:', data);
});

--> GOT DATA: <Buffer 3c 78 6d 6c 3e 3c 65 72 72 6f 72 3e 3c 6d 65 73 73 61 67 65 3e 68 65 69 3c 2f 6d 65 73 73 61 67 65 3e 3c 2f 65 72 72 6f 72 3e 3c 2f 78 6d 6c 3e>

client.get('http://localhost:8080/js', function (data, response) {
    console.log('GOT DATA:', data);
});

--> GOT DATA: { error: 'error!' }

I have a basic express server set up to test it.

var express = require('express');
var app = express();

app.get('/js', function (req, res) {
    res.status(500).send({
        error: 'error!'
    });
});

app.get('/xml', function(req, res) {
    res.set('Content-Type', 'text/xml');
    res.status(500).send(
        '<xml>' +
            '<error>error!</error>' +
        '</xml>'
    );
});

app.listen(8080);

Is this expected behaviour?

aacerox commented 7 years ago

Hi no it wasn't the expected behaviour. this bug is fixed in the next version.

thnx.

timmeade commented 7 years ago

I just hit this on the current version. Some calls are generating a buffer.