I'll simplify this question here as best as I can:
First on the client side:
let textToSend = "Shouldn't it work";
console.log(textToSend) // Logs: Shouldn't it work (with apostrophe)
let response = await fetch("http://localhost:4000/somewhere",{
method:"POST",
headers:{
"message" : textToSend
}
})
Then on the server:
exports.function = function(req,res){
let messageText = req.get("message");
console.log(messageText); //Logs: Shouldn
}
So pretty much my problem is, the string gets cut to "Shouldn" since there's an apostrophe after the n. I've tried adding a backslash before the apostrophe but still does the same thing 🤔 Any ideas? I've also tried a double backslash.
Also, in the PostgreSQL server it's connected to, the datatype is varchar.
Doing some more research on URI encoding, I found that decoding does not work on node for some reason 🤔. However, when fetching the results, the encoded text can be decoded on the client side when needed, example:
Client Side:
let textToSend = encodeURI("Shouldn't it work");
console.log(textToSend); //Logs: Shouldn%E2%80%99t%20it%20work
let response = await fetch("http://localhost:4000/somewhere",{
method:"POST",
headers:{
//lets send this message to the server
"message" : textToSend
}
})
Server Side:
let message = req.get("message");
console.log(decodeURI(message)); //Logs: Shouldn%E2%80%99t%20it%20work
//Notice how decoding doesn't work here for some reason
Back on client side:
let response = await fetch("http://localhost:4000/getMessages",{
method:"GET",
})
if(response.ok){
try{
let json = await response.json();
console.log(decodeURI(json)); //Logs: Shouldn't it work
}catch(e){
console.log(e)
}
}
In summary, encode it when sending, decode it when receiving it, don't decode it on the server side.