I have to retrieve XML data from a server. I'm using Fetch since I need an async/await compatible way of making HTTP requests.

However using resp.text() when retrieving the data via fetch causes it to escape all quotes and newlines.

Here's what the result should look like:

<?xml version="1.0" encoding="UTF-8"?><data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <value var_id="v04" xsi:type="ELEMENT">hello</value>

And here's what it actually looks like:

"<?xml version=\"1.0\" encoding=\"UTF-8\"?><data xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n      <value var_id=\"v04\" xsi:type=\"ELEMENT\">hello</value>\n</data>\n"

Anyway I can prevent this and receive the raw XML? Alternatively, anyway to parse it back to its original form? Thanks.

1 Answers

Benjamin Gruenbaum On Best Solutions

Doing .text in node-fetch just does:

    return consumeBody.call(this).then(buffer => buffer.toString());

This is literally the source code of .text

Which is just a Node Buffer#toString() - that does no escaping one way or another.

Node.js does escape strings before printing them so you can use them and copy-paste them.

Rest assured - this is just a UI visibility feature - not an actual text escaping change :)