通过RestAPI获取strapi的数据

一、访问 strapi 数据
1、获取内容
# 获取 restaurants 所有内容
GET http://localhost:1337/restaurants

# 获取 restaurants 中 id = 1 的内容
GET http://localhost:1337/restaurants/1

# 获取 restaurants 中内容的数量
GET http://localhost:1337/restaurants/count
2、过滤
# 获取 firstName 等于 John 的内容
GET /users?firstName=John
or
GET /users?firstName_eq=John

# 获取 price ≥ 3 的餐馆
GET /restaurants?price_gte=3

# 获取 id 为 3 或 6 或 8 的餐馆
GET /restaurants?id_in=3&id_in=6&id_in=8
Filter Description
No suffix or eq Equal
ne Not equal
lt Less than
gt Greater than
lte Less than or equal to
gte Greater than or equal to
in Included in an array
nin Not included in an array
contains Contains
ncontains Doesn’t contain
containss Contains, case sensitive
ncontainss Doesn’t contain, case sensitive
null Is null or not null
3、where
GET /restaurants?_where[price_gte]=3
GET /restaurants?_where[0][price_gte]=3&[0][price_lte]=7
4、and
const query = qs.stringify({
  _where: [{ stars: 1 }, { pricing_lte: 20 }],
});

await request(`/restaurants?${query}`);
// GET /restaurants?_where[0][stars]=1&_where[1][pricing_lte]=20
5、or
# 用法一
const query = qs.stringify({ _where: { _or: [{ stars: 1 }, { pricing_gt: 30 }] } });

await request(`/restaurant?${query}`);
// GET /restaurants?_where[_or][0][stars]=1&_where[_or][1][pricing_gt]=30

# 用法二
GET /restaurants?stars=1&stars=2

# 用法三
const query = qs.stringify({ _where: { stars: [1, 2] } });

await request(`/restaurant?${query}`);
// GET /restaurants?_where[stars][0]=1&_where[stars][1]=2
 
6、and + or
# 用法一
const query = qs.stringify({
  _where: {
    _or: [
      [{ stars: 2 }, { pricing_lt: 80 }], // implicit AND
      [{ stars: 1 }, { pricing_gte: 50 }], // implicit AND
    ],
  },
});

await request(`/restaurants?${query}`);
// GET /restaurants?_where[_or][0][0][stars]=2&_where[_or][0][1][pricing_lt]=80&_where[_or][1][0][stars]=1&_where[_or][1][1][pricing_gte]=50
# 用法二
const query = qs.stringify({
  _where: {
    _or: [
      [{ stars: 2 }, { pricing_lt: 80 }], // implicit AND
      [{ stars: 1 }, { 'categories.name': 'French' }], // implicit AND
    ],
  },
});

await request(`/restaurants?${query}`);
// GET /restaurants?_where[_or][0][0][stars]=2&_where[_or][0][1][pricing_lt]=80&_where[_or][1][0][stars]=1&_where[_or][1][1][categories.name]=French
7、sort
GET /users?_sort=email:ASC to sort by ascending order

GET /users?_sort=email:DESC to sort by descending order

GET /users?_sort=email:ASC,dateField:DESC
GET /users?_sort=email:DESC,username:ASC
8、limit
  • The default limit is 100

  • You can require the full data set by passing a limit equal to -1

GET /users?_limit=30
9、Start - 分页
GET /users?_start=10&_limit=10
10、获取已发布或草稿文章
# 获取文章 = 已发布
GET /articles
or
GET /articles?_publicationState=live

# 获取文章 = 已发布 + 草稿
GET /articles?_publicationState=preview

二、参考文档
上一篇:如何使用github


下一篇:开源问答系统调研