ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

通过RestAPI获取strapi的数据

2021-11-20 09:04:42  阅读:214  来源: 互联网

标签:GET RestAPI 获取 strapi pricing stars query where restaurants


一、访问 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
  • 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

二、参考文档

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有