Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
How to pass a string with an apostrophe as a header to a node server with a PostgreSQL database?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!