Skip to content

Instantly share code, notes, and snippets.

@pawarvijay
Last active January 9, 2022 17:51
Show Gist options
  • Save pawarvijay/057fb5a50342aab619c14da3175ed870 to your computer and use it in GitHub Desktop.
Save pawarvijay/057fb5a50342aab619c14da3175ed870 to your computer and use it in GitHub Desktop.
how to send http post request by curl to node expressjs
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function (req, res, next) {
console.log('aeee ' + JSON.stringify(req.body))
next()
})
app.post('/hello', function (req, res) {
console.log(JSON.stringify(req.body.hiee) + ' yoooooo');
res.send('hello : ' + JSON.stringify(req.body));
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
TRY 1
-----
with line 6
TRY 1.1
-------
Command
curl --data "foo=bar&baz=qux" http://localhost:3000/hello
Output
hello : {"foo":"bar","baz":"qux"}
TRY 1.2
-------
Command
curl -d '{"foo":"bar","baz ":"qux"}' -H "Content-Type: application/json" http://127.0.0.1:3000/hello --verbose
Output
hello : {"foo":"bar","baz":"qux"}
TRY 1.3
-------
Command
curl -d '{"foo":"bar","baz ":"qux"}' http://127.0.0.1:3000/hello --verbose
Output
hello : {"{\"foo\":\"bar\",\"baz \":\"qux\"}":""}
TRY 2
-----
without line 6
Command
curl -d '{"foo":"bar","baz ":"qux"}' -H "Content-Type: application/json" http://127.0.0.1:3000/hello --verbose
Output
hello : {"foo":"bar","baz":"qux"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment