1

I have NodeJS code which looks like this

app.post("/calc", (req, res) => {
    res.status(200).json({
        success: true, 
        msg: req.body.msg,
    })
})

I can't use Postman that's why I want to test this endpoint using curl. How to do this?

I tried code like this but it returned an error

curl -X POST http://localhost:3000/calc -H "Content-Type: application/json" -d '{"msg": 123456}'

Error:

 Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type "System.String" to type "S
ystem.Collections.IDictionary".
Javid
  • 13
  • 3
  • Does this answer your question? [Invoke-WebRequest : Cannot bind parameter 'Headers'](https://stackoverflow.com/questions/45183355/invoke-webrequest-cannot-bind-parameter-headers) – Remus Crisan Mar 14 '23 at 09:23

2 Answers2

1

This looks like Windows tricks you up so that you use the curl Invoke-webrequest alias instead of the real curl tool.

Try invoking curl as curl.exe to avoid that nastiness.

Also: drop -X POST from that command line, -d already means POST.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
0

I think you were missing this code on your express side.

Because Postman did not work either.

app.use(express.json());

Demo code, save as server.js file

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors())

// for form-data of Body
const multer = require('multer');
const forms = multer();
app.use(forms.array()); 

// for x-www-form-urlencoded of Body
app.use(express.urlencoded({ extended: true }))

// for raw JSON of Body
app.use(express.json()); 

app.post('/calc', (req, res)=> {
    res.status(200).json({
        success: true, 
        msg: req.body.msg,
    })
})

app.listen(3000, () => { console.log("Listening on : 3000") })

Install dependencies

npm install express cors multer

Run it by node

node server.js

Test if by curl

curl --location 'http://localhost:3000/calc' \
--silent \
--header 'Content-Type: application/json' \
--data '{
    "msg": 123456
}'

Result

enter image description here

Test by Postman

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14