Skip to content

JSONPlaceholder posts API 文档

官网地址 https://jsonplaceholder.typicode.com/

介绍

JSONPlaceholder 是一个免费的模拟 RESTful API。本文档详细描述了如何与 posts 资源进行交互。

基本信息

  • Base URL: https://jsonplaceholder.typicode.com
  • Media Type: application/json

错误码

HTTP 状态码描述
400请求的数据格式错误
404资源未找到
500服务器内部错误

API 接口描述

1. 获取所有 posts

请求

  • Method: GET
  • URL: /posts
  • Headers:
    • Accept: application/json

响应

  • 成功响应:
    • Code: 200 OK
    • Content-Type: application/json
    • Body:
    json
    [
      {
        "userId": 1,
        "id": 1,
        "title": "...",
        "body": "..."
      },
      ...
    ]

2. 获取单个 post

请求

  • Method: GET
  • URL: /posts/:id
  • Headers:
    • Accept: application/json

响应

  • 成功响应:
    • Code: 200 OK
    • Content-Type: application/json
    • Body:
    json
    {
      "userId": 1,
      "id": 1,
      "title": "...",
      "body": "..."
    }

3. 创建一个新的 post

请求

  • Method: POST
  • URL: /posts
  • Headers:
    • Accept: application/json
    • Content-Type: application/json
  • Body:
    json
    {
      "title": "[string]",
      "body": "[string]",
      "userId": [integer]
    }

响应

  • 成功响应:
    • Code: 201 Created
    • Content-Type: application/json
    • Body:
    json
    {
      "title": "...",
      "body": "...",
      "userId": ...,
      "id": ...
    }

4. 更新一个 post

请求

  • Method: PUT
  • URL: /posts/:id
  • Headers:
    • Accept: application/json
    • Content-Type: application/json
  • Body:
    json
    {
      "id": [integer],
      "title": "[string]",
      "body": "[string]",
      "userId": [integer]
    }

响应

  • 成功响应:
    • Code: 200 OK
    • Content-Type: application/json
    • Body:
    json
    {
      "title": "...",
      "body": "...",
      "userId": ...,
      "id": ...
    }

5. 删除一个 post

请求

  • Method: DELETE
  • URL: /posts/:id
  • Headers:
    • Accept: application/json

响应

  • 成功响应:
    • Code: 200 OK
    • Content-Type: application/json
    • Body: {}

6. 分页查询

请求

  • Method: GET
  • URL: /posts
  • Headers:
    • Accept: application/json
  • Query Parameters:
    • _limit=[integer],每页显示的数量
    • _start=[integer],从哪个 post 开始

响应

  • 成功响应:
    • Code: 200 OK
    • Content-Type: application/json
    • Body:
    json
    [
      ...
    ]

示例

  1. 获取前 5 个 posts:

    GET https://jsonplaceholder.typicode.com/posts?_limit=5
  2. 获取 ID 为 3 的 post:

    GET https://jsonplaceholder.typicode.com/posts/3
  3. 更新 ID 为 4 的 post:

    PUT https://sonplaceholder.typicode.com/posts/4