Reputation: 85
When I execute this request curl -X POST -H "Content-Type:application/json" http://localhost:3000/messages -d '{"text":"Hi again, World"}'
on cURL to an express I get this error
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token ' in JSON at position 0<br> at JSON.parse (<anonymous>)<br> at createStrictSyntaxError (C:\Users\Atom\node_modules\body-parser\lib\types\json.js:158:10)<br> at parse (C:\Users\Atom\node_modules\body-parser\lib\types\json.js:83:15)<br> at C:\Users\Atom\node_modules\body-parser\lib\read.js:121:18<br> at invokeCallback (C:\Users\Atom\node_modules\raw-body\index.js:224:16)<br> at done (C:\Users\Atom\node_modules\raw-body\index.js:213:7)<br> at IncomingMessage.onEnd (C:\Users\Atom\node_modules\raw-body\index.js:273:7)<br> at IncomingMessage.emit (events.js:327:22)<br> at endReadableNT (internal/streams/readable.js:1327:12)<br> at processTicksAndRejections (internal/process/task_queues.js:80:21)</pre>
</body>
</html>
I don't know what I did wrong, since I'm just copy pasting stuff from this tutorial. I did my search and cannot find any syntax error on the curl request, still that's what the error is about, I understand that the ' is the single quote ('). Any help will be highly appreciated. My app.js file:
var express = require('express');
var app = express();
const { v4: uuidv4 } = require('uuid');
require('dotenv').config();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
let users = {
1: {
id: '1',
username: 'Robin Wieruch',
},
2: {
id: '2',
username: 'Dave Davids',
},
};
let messages = {
1: {
id: '1',
text: 'Hello World',
userId: '1',
},
2: {
id: '2',
text: 'By World',
userId: '2',
},
};
app.get('/users', (req, res) => {
return res.send(Object.values(users));
});
app.get('/users/:userId', (req, res) => {
return res.send(users[req.params.userId]);
});
app.post('/messages', (req, res) => {
const id = uuidv4();
const message = {
id,
text: req.body.text
};
messages[id] = message;
return res.send(message);
});
app.get('/messages', (req, res) => {
return res.send(Object.values(messages));
});
app.get('/messages/:messageId', (req, res) => {
return res.send(messages[req.params.messageId]);
});
app.post('/users', (req, res) => {
return res.send('POST HTTP method on user resource');
});
app.put('/users/:userId', (req, res) => {
return res.send(
`PUT HTTP method on user/${req.params.userId} resource`,
);
});
app.delete('/users/:userId', (req, res) => {
return res.send(
`DELETE HTTP method on user/${req.params.userId} resource`,
);
});
app.listen(process.env.PORT, () =>
console.log(`Example app listening on port ${process.env.PORT}!`),
);
Upvotes: 3
Views: 2283
Reputation: 1226
further investigation shows that if you are using a windows cmd terminal you may need to escape the double quotes and avoid single quotes, so for instance on git bash the following works
curl -X POST -H "Content-Type:application/json" -d '{"route": "Test route"}' http://192.168.1.xx:xxxx/
but not on windows cmd. For that I used:
curl -X POST -H "Content-Type:application/json" -d "{\"route\": \"Test route\"}" http://192.168.1.xx:xxxx/
Note change the xx xxxx to your own url, port
The source for this insight was https://github.com/biofects/Google-Home-Messages/issues/3
Gordon
Upvotes: 6
Reputation: 28
I don't know what terminal you used but I was having the same error when using node.js command prompt. The issue was fixed in my case by using bash terminal instead.
Upvotes: 1