2

I am testing a server.js file through curl from cmd for post request where I am getting the above error. I saw many related question and answers but nothing solves my problem. Please provide me the solution.

This is my server.js

var app = require('express')();
var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json

app.post('/data', function (req, res) {
console.log(req.body);
res.end();  
});

app.listen(3000);

and This is curl code from cmd,

C:\Users\user\Downloads\Compressed\curl.exe -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data

This is the error I am getting,

error image. click here to see it.

1 Answer 1

4

You are using following body

'{\"key1\":\"value1\", \"key2\":\"value2\"}'

Use following instead

"{\"key1\":\"value1\", \"key2\":\"value2\"}" 
Sign up to request clarification or add additional context in comments.

5 Comments

sorry, i have uploaded wrong picture. I was doing the same from first (without escaping). check the image once again.
Try to use "{\"key1\":\"value1\", \"key2\":\"value2\"}"
That wasn't worked. i have uploaded the wrong image which was tried by me. That too, didn't work.
I have executed following commend curl.exe -d "{\"key1\":\"value1\", \"key2\":\"value2\"}" -H "Content-Type: application/json" -X POST http://localhost:3000/data and it is working fine. Can you copy this and paste it and then try?
Please note I have replace single quotes ' with double quotes "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.