Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda