Sending Post request with curl using nested objects

Viewed 5680

I have a mongoose schema in which i have to do a POST PUT DELETE request on. However when i send the curl command the server outputs the following

{ ValidationError: data validation failed: categories.url_name: Path `categories.url_name` is required., categories.name: Path `categories.name` is required.

It tells me the data wasnt sent through the curl command correctly. So i would like to know how to properly write a curl post with nested json objects

My mongoose schema is the following :

var DataSchema = new mongoose.Schema({
categories: {
    name :  {
        type : String,
        required : true
    },
    url_name : {
        type : String,
        required : true
    }
  }

})

And my curl command is this one

curl -H 'Content-Type: application/json' -X POST -d '{“categories”:” { name :1, url_name :example }” ' http://localhost:4200/add

Is there something wrong with the json or is it the schema i´ve created?

2 Answers

Your curl command had extra double quotes. Please try:

curl -H 'Content-Type: application/json' -X POST -d '{"categories": { "name" :1, "url_name" : "example" }}' http://localhost:4200/add

Your curl request is not formatted correctly. Try:

curl \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{"categories": { "name" :1, "url_name": "example" }}' \
  http://localhost:4200/add
Related