其他分享
首页 > 其他分享> > 通过RestAPI获取strapi的数据

通过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
FilterDescription
No suffix or eqEqual
neNot equal
ltLess than
gtGreater than
lteLess than or equal to
gteGreater than or equal to
inIncluded in an array
ninNot included in an array
containsContains
ncontainsDoesn’t contain
containssContains, case sensitive
ncontainssDoesn’t contain, case sensitive
nullIs 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
GET /users?_limit=30
9、Start - 分页
GET /users?_start=10&_limit=10
10、获取已发布或草稿文章
# 获取文章 = 已发布
GET /articles
or
GET /articles?_publicationState=live

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

二、参考文档

标签:GET,RestAPI,获取,strapi,pricing,stars,query,where,restaurants
来源: https://blog.csdn.net/fanlehai/article/details/121347828