Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda