【问题标题】:Sending Post request with curl using nested objects使用嵌套对象发送带有 curl 的 Post 请求
【发布时间】:2019-10-27 09:20:37
【问题描述】:

我有一个猫鼬模式,我必须在其中执行 POST PUT DELETE 请求。但是,当我发送 curl 命令时,服务器会输出以下内容

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

它告诉我数据没有通过 curl 命令正确发送。所以我想知道如何用嵌套的 json 对象正确地写一篇 curl 文章

我的猫鼬模式如下:

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

})

我的 curl 命令就是这个

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

json 有问题还是我创建的架构有问题?

【问题讨论】:

    标签: node.js curl mongoose


    【解决方案1】:

    您的curl 命令有额外的双引号。请尝试:

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

    【讨论】:

    • 它在 json 中的位置 40 处返回意外的令牌 e
    • 我在“example”周围添加了引号
    【解决方案2】:

    您的 curl 请求格式不正确。试试:

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

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 2011-04-12
      • 2018-06-29
      • 1970-01-01
      • 2022-08-14
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多