String formatting lost from URL string capture when sending from client to node js back-end

35 Views Asked by At

I'm trying to get a simple formatted string (as follows) to my node js back-end.

'one\ntwo\nthree'

Instead, when printing to console from node js I get:

'onetwothree'

I SHOULD get:

one

two

three

In fact, when I take the exact same printed console message and paste it in the Node js code to overwrite that from the client, it's printed correctly!..

My client code:

let content = '1\n2\n3';

fetch(`/api/post?textCapture=${content}`)

Server code:

app.get('/api/post', (req,res) => {

    let textCapture = req.query.textCapture;

    console.log("textCapture: " + textCapture);

}

Any ideas how I can resolve??

Thanks,

As above.

Also tried sending the string via the body of the Fetch from client, but I got a 404 not-found error.

UPDATE:

Here's an interesting find looking at ASCII table with buffer reads from my strings...

the incorrect print gives:

k \n ls \n t \n
<Buffer 6b 5c 6e 6c 73 5c 6e 74 5c 6e>

When I write out / copy paste from the console print and overwrite the variable I get the following correct print in the console:

k
ls
t
<Buffer 6b 0a 6c 73 0a 74 0a>

In the incorrect version a '5c' equates to '' and the '6e' to 'n'. In the correct version a '0a' indicates the joint '\n'

Why is this happening...?

0

There are 0 best solutions below