将 Postman 请求发送到 json 服务器时出现问题

时间:2021-06-30 16:56:10

标签: android json rest server postman

我想向我的 json 服务器发送一个帖子以更改数据库(我想用“post”向它添加信息)。当我这样做时,json 服务器添加了一个带有下一个整数的 id 的部分,但没有我想要添加的所有内容。没有错误消息,一切就像工作一样,但事实并非如此。我在谷歌上进行了研究以解决我的问题,但所有为初学者神经系统展示邮递员系统的教程都有这个问题,并且所有领域都在教程中正确表达(但在我的情况下不是!)。我做这一切是因为我正在构建一个 REST api

这就是我使用 Windows10 cmd 启动我的 json 服务器的方式(我使用 localhost:3000 与邮递员连接)

json-server --watch db.json

这是我的 db.json:

    {
  "posts": [
    {
      "id": 1,
      "title": "json-server",
      "author": "typicode"
    }
  ],
  "comments": [
    {
      "id": 1,
      "body": "some comment",
      "postId": 1
    }
  ],
  "profile": {
    "name": "typicode"
  }
}

这是我启动json服务器时的代码

     \{^_^}/ hi!

  Loading db.json
  Done

  Resources
  http://localhost:3000/posts
  http://localhost:3000/comments
  http://localhost:3000/profile

  Home
  http://localhost:3000

1 个答案:

答案 0 :(得分:0)

请确保在 postman 中设置了以下选项:

  1. 检查您是否已将 Header 设置为“Content-Type: application/json”
  2. 确保正文类型设置为“原始”,类型下拉列表设置为“JSON”
  3. POST 正文必须是有效的 JSON。