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

299
Vistas
In my JavaScript server, why does my posted data have braces and quotes around it?

I have a simple JavaScript server to process a POST command. But my post data shows up surrounded with braces and quotes. I'm using curl to send this:

curl --data {title:'Odessey',author:'Homer'} localhost:4444/test/add

But my server gets the posted data like this:

{ "{title:'Odessey',author:'Homer'}": '' }

When I send it back to the client, it shows up with spaces removed and quotes converted:

{"{title:'Odessey',author:'Homer'}":""}

Curiously, if I put quotes around the string in curl, I get exactly the same thing:

curl --data "{title:'Odessey',author:'Homer'}" localhost:4444/test/add

I'm running this on Windows 10. The problem doesn't seem to be with curl, because I wrote another (clumsier) server without using express, and this problem didn't show up. Also, I've tried curl's other options for sending data, like --data.ascii and --data.raw, but none of them help. So the problem must be with the express server.

Also, I log the req's Content-Type before processing the POST DATA. By default, it prints out application/x-www-form-urlencoded. At the suggestion of other users, I changed the Content-Type header in the curl statement by saying curl -H Content-Type:application/json, but that didn't help. The log statement confirms that it received this header, but then it fails to read the data at all, logging req.body: {}.

Here's my server code:

const express = require('express');
const app = express();

// taking this block out doesn't help. Without it, I don't get any data.  
app.use(
    express.urlencoded({ extended: true })
);

// Changing this to express.text() doesn't help. Nor does removing it.
app.use(express.json());

app.post('/test/add', (req, res) => {
    let hdr = "Content-Type";
    console.log(hdr, req.header(hdr)); 
    console.log("req.body:", req.body);
    res.send(req.body);
})

var server = app.listen(4444, function() {
    var urlStr = "http://" + host + ':' + port;
    console.log ("Example app listeneing at", urlStr);
})

Note: I have updated this slightly since I first posted it.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should be able to do this by adding the following header: -H 'Content-Type: application/json'. It is receiving it as a GET parameter (something like localhost:4444/test/add?{title:'Odessey',author:'Homer'}).

about 4 years ago · Juan Pablo Isaza Denunciar

0

Mario Martín Moreno had a very useful suggestion to add a -H option to my curl command. This was correct, but the form wasn't quite right. @dandavis was correct that only double quotes were valid for the keys. And @tevemader was right that I was using a header file incorrectly. But the crucial detail was that I needed to escape my quote characters with a backslash. So the full curl line looked like this:

curl --data {\"title\":\"Odessey\",\"author\":\"Homer\"} -H Content-Type:application/json localhost:4444/test/add

Caveats: Single quotes don't work even on just the values, and even with the backslash to escape them. And I still don't know why I needed to do this here, but not with my earlier server that didn't use express. But at least I know how to use curl to send my data. Thank you to everyone who responded with helpful suggestions.

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