创建文章

基本信息

接口名称: 创建文章

接口类型: 数据上传

接口说明: 在某个站点内新建一篇文章

详细信息

URL

https://www.baklib.com/api/v1/articles

完整URL示例

https://www.baklib.com/api/v1/articles

HTTP请求方式

POST

授权认证方式

默认方式,需要添加请求header

Authorization: Bearer <token>

请求参数

参数 必选 类型及范围 说明
tenant_id true String 站点id
name true String 文章标题
channel_id false String 文章归属的栏目id
tag_list false Array[String] 标签
content_type false String 内容格式,支持'markdown'(即content传入markdorn格式的字符串内容) 、'paragraph'(即content传入文本字符串内容)、‘raw’(即content传入baklib使用的编辑器editor.js格式的字符串内容),默认为 'paragraph'。
content false String 内容(数据类型均为字符串,与content_type对应,有三种形式,下面分别举例说明。)
description false String 文章描述
status false Integer 发布状态 0=> '草稿',1=>'已发布',默认为草稿状态。
identifier false String 唯一标识
access_level false Integer 访问等级
password false String 访问密码
fake_visits_count false Integer 虚拟访问量

示例代码

传入content_type为paragraph的数据

//传入content_type为paragraph

var data = JSON.stringify({
  "tenant_id": "aaa24dbe-a17b-4439-911d-176e0149cf76",
  "name": "content_type为paragraph",
  "channel_id": "9fca4e37-ce6d-4ddb-8082-896cebb509ce",
  "status": "0",
  "description": "这是描述",
  "tag_list": [
    "Ruby",
    "Rails"
  ],
  "fake_visits_count": 800,
  "password": "123",
  "content": "测试文本",
  "content_type": "paragraph",
  "access_level":  0,
    "identifier": "create-article"
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://www.baklib.com/api/v1/articles");
xhr.setRequestHeader("Authorization", "Bearer 8486df9cb8a93ea843702141065c4a8b714602b45e7c4a6bb26a26ea3d69de6");
xhr.setRequestHeader("Content-Type", "application/json");


xhr.send(data);

返回结果

{
    "code": 0,
    "status": "success",
    "message": {
        "id": "d4a305ef-5ecc-4924-a882-9e840ed51e01",
        "parent_id": "9fca4e37-ce6d-4ddb-8082-896cebb509ce",
        "name": "content_type为paragraph",
        "identifier": "create-article"
        "description": "这是描述",
        "content": {
            "time": 1668678541000,
            "version": "2.18.0-5.8",
            "blocks": [
                {
                    "type": "paragraph",
                    "data": {
                        "text": "测试文本"
                    }
                }
            ]
        },
        "ordinal": 0,
        "status": 0,
        "status_name": "草稿",
        "kind": 2,
        "kind_name": "文章",
        "slug": "bfc9/4177",
        "tenant_id": "aaa24dbe-a17b-4439-911d-176e0149cf76",
        "visits_count": 0,
        "fake_visits_count": 800,
        "created_at": "2022-11-17T17:49:01.326+08:00",
        "updated_at": "2022-11-17T17:49:01.285+08:00",
        "created_by": "822ca2a8-b3b7-4d7c-aaed-8cd3b221373f",
        "updated_by": "822ca2a8-b3b7-4d7c-aaed-8cd3b221373f",
        "password": "123",
        "access_level": 0,
        "template": "article.default",
        "variables": {},
        "variables_description": [],
        "tag_list": [
            "Ruby",
            "Rails"
        ],
        "frontend_url": "XXX",
        "edit_url": "XXX"
    }
}

传入content_type为markdown的数据

//传入content_type为markdown类型的数据


var data = JSON.stringify({
  "tenant_id": "aaa24dbe-a17b-4439-911d-176e0149cf76",
  "name": "content_type为markdown",
  "channel_id": "9fca4e37-ce6d-4ddb-8082-896cebb509ce",
  "status": "0",
  "description": "这是描述",
  "tag_list": [
    "Ruby",
    "Rails"
  ],
  "fake_visits_count": 800,
  "password": "123",
  "content": "### 基本信息\n\n**接口名称:** 创建文章\n\n**接口类型:** 数据上传\n\n**接口说明:** 在某个站点内新建一篇文章\n\n### 详细信息\n\n**URL**\n\n`http://www.baklib.com/api/v1/articles`\n\n**完整URL示例**\n\n`http://www.baklib.com/api/v1/articles`\n\n**HTTP请求方式**\n\nPOST\n\n**授权认证方式**\n\n默认方式,需要添加请求header\n```http\nAuthorization: Bearer &lt;token>\n```",
  "content_type": "markdown",
  "access_level": 0,
  "identifier": "create-article"
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://www.baklib.com/api/v1/articles");
xhr.setRequestHeader("Authorization", "Bearer 8486df9cb8a93ea843702141065c4a8b714602b45e7c4a6bb26a26ea3d69de6");
xhr.setRequestHeader("Content-Type", "application/json");



xhr.send(data);

返回结果

{
    "code": 0,
    "status": "success",
    "message": {
        "id": "76d97621-9414-49fd-8218-ac68f7684a3d",
        "parent_id": "9fca4e37-ce6d-4ddb-8082-896cebb509ce",
        "name": "content_type为markdown",
        "identifier": "create-article"
        "description": "这是描述",
        "content": {
            "time": 1668678353000,
            "version": "2.18.0-5.8",
            "blocks": [
                {
                    "type": "markdown",
                    "data": {
                        "text": "### 基本信息\n\n**接口名称:** 创建文章\n\n**接口类型:** 数据上传\n\n**接口说明:** 在某个站点内新建一篇文章\n\n### 详细信息\n\n**URL**\n\n`http://www.baklib.com/api/v1/articles`\n\n**完整URL示例**\n\n`http://www.baklib.com/api/v1/articles`\n\n**HTTP请求方式**\n\nPOST\n\n**授权认证方式**\n\n默认方式,需要添加请求header\n```http\nAuthorization: Bearer <token>\n```"
                    }
                }
            ]
        },
        "ordinal": 0,
        "status": 0,
        "status_name": "草稿",
        "kind": 2,
        "kind_name": "文章",
        "slug": "bfc9/de80",
        "tenant_id": "aaa24dbe-a17b-4439-911d-176e0149cf76",
        "visits_count": 0,
        "fake_visits_count": 800,
        "created_at": "2022-11-17T17:45:53.958+08:00",
        "updated_at": "2022-11-17T17:45:53.912+08:00",
        "created_by": "822ca2a8-b3b7-4d7c-aaed-8cd3b221373f",
        "updated_by": "822ca2a8-b3b7-4d7c-aaed-8cd3b221373f",
        "password": "123",
        "access_level": 0,
        "template": "article.default",
        "variables": {},
        "variables_description": [],
        "tag_list": [
            "Ruby",
            "Rails"
        ],
        "frontend_url": "XXX",
        "edit_url": "XXX"
    }
}

传入content_type为raw的数据

//传入content_type为raw类型的数据

var data = JSON.stringify({
  "tenant_id": "aaa24dbe-a17b-4439-911d-176e0149cf76",
  "name": "content_type为raw",
  "channel_id": "9fca4e37-ce6d-4ddb-8082-896cebb509ce",
  "status": "0",
  "description": "这是描述",
  "tag_list": [
    "Ruby",
    "Rails"
  ],
  "fake_visits_count": 800,
  "password": "123",
  "content": "{\"time\":1668669785851,\"blocks\":[{\"id\":\"guNSvTDzsg\",\"type\":\"paragraph\",\"data\":{\"text\":\"Hello, Baklib!\",\"alignment\":\"align-left\"}},{\"id\":\"tRi5OCbGaX\",\"type\":\"qiniuImage\",\"data\":{\"url\":\"http://dev.bk-cdn.com/t/aaa24dbe-a17b-4439-911d-176e0149cf76/u/822ca2a8-b3b7-4d7c-aaed-8cd3b221373f/1668668621876/v2-454fba3b1159d364b448f77e092aef74.jpg\",\"caption\":\"说明\",\"withBorder\":false,\"withBackground\":false,\"stretched\":false,\"isHistoryImage\":false}},{\"id\":\"f_J2OncGRk\",\"type\":\"paragraph\",\"data\":{\"text\":\"\",\"alignment\":\"align-left\"}}],\"version\":\"2.22.2\"}",
  "content_type": "raw",
   "access_level": 0,
    "identifier": "create-article"
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://www.baklib.com/api/v1/articles");
xhr.setRequestHeader("Authorization", "Bearer 8486df9cb8a93ea843702141065c4a8b714602b45e7c4a6bb26a26ea3d69de6");
xhr.setRequestHeader("Content-Type", "application/json");

xhr.send(data);

返回结果

{
    "code": 0,
    "status": "success",
    "message": {
        "id": "d82f0253-6200-4433-a9ac-151946291a8f",
        "parent_id": "9fca4e37-ce6d-4ddb-8082-896cebb509ce",
        "name": "content_type为raw",
       "identifier": "create-article"
        "description": "这是描述",
        "content": {
            "time": 1668669785851,
            "blocks": [
                {
                    "id": "guNSvTDzsg",
                    "type": "paragraph",
                    "data": {
                        "text": "Hello, Baklib!",
                        "alignment": "align-left"
                    }
                },
                {
                    "id": "tRi5OCbGaX",
                    "type": "qiniuImage",
                    "data": {
                        "url": "http://dev.bk-cdn.com/t/aaa24dbe-a17b-4439-911d-176e0149cf76/u/822ca2a8-b3b7-4d7c-aaed-8cd3b221373f/1668668621876/v2-454fba3b1159d364b448f77e092aef74.jpg",
                        "caption": "说明",
                        "withBorder": false,
                        "withBackground": false,
                        "stretched": false,
                        "isHistoryImage": false
                    }
                },
                {
                    "id": "f_J2OncGRk",
                    "type": "paragraph",
                    "data": {
                        "text": "",
                        "alignment": "align-left"
                    }
                }
            ],
            "version": "2.22.2"
        },
        "ordinal": 0,
        "status": 0,
        "status_name": "草稿",
        "kind": 2,
        "kind_name": "文章",
        "slug": "bfc9/ba72",
        "tenant_id": "aaa24dbe-a17b-4439-911d-176e0149cf76",
        "visits_count": 0,
        "fake_visits_count": 800,
        "created_at": "2022-11-17T17:41:39.543+08:00",
        "updated_at": "2022-11-17T17:41:39.502+08:00",
        "created_by": "822ca2a8-b3b7-4d7c-aaed-8cd3b221373f",
        "updated_by": "822ca2a8-b3b7-4d7c-aaed-8cd3b221373f",
        "password": "123",
        "access_level": 0,
        "template": "article.default",
        "variables": {},
        "variables_description": [],
        "tag_list": [
            "Ruby",
            "Rails"
        ],
        "frontend_url": "XXX",
        "edit_url": "XXX"
    }
}

返回字段说明

返回值字段 字段类型 说明
code Integer 状态码
status String 返回状态
message Hash 返回数据主体
    id String 文章id
    parent_id String 栏目id
    name String 文章标题
    identifier String 栏目唯一标识符
    description String 文章描述
    content Jsonb 文章内容
        time Integer 文章创建时间码
        verison String 编辑器相关版本
        blocks Array[String] 对应编辑器中显示的区块
            type String 内容格式类型(paragraph=>段落,quote=>引用,qiniuImage=>图片)
            id String 区块id
            data Jsonb 区块数据
                text String 文本
                alignment String 对齐方式
                url String 图片地址(传图片时)
                caption String 图片说明(传图片时)
                withBorder Boolean 是否加边框(传图片时)
                withBackground Boolean 是否为背景(传图片时)
                stretched Boolean 是否拉伸(传图片时)
                isHistoryImage Boolean 是否为历史图片(传图片时)
    ordinal String 文章排序
    status Integer 文章状态, 0=> '草稿',1=>'已发布';默认为草稿状态。
    status_name String 栏目状态中文名称
    kind Integer 文章类型标志
    kind_name String 文章类型名称
    slug String 文章路径
    tenant_id String 站点id
    visit_count Integer 访问总数
    fake_visits_count Integer 虚拟访问量
    created_at Datetime 创建时间
    updated_at Datetime 更新时间
    created_by String 创建者id
    updated_by String 更新者id
    password String 密码
    access_level Integer 访问等级
    template String 文章模板
    variables Jsonb 其他变量
    variables_description Array 变量描述
    tag_list Array 标签结果集
    frontend_url String 前端访问地址
    edit_url String 编辑地址
2022-11-23
3 1