简体   繁体   中英

Sending Post request with curl using nested objects

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM