You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Fetch在REST API的GET请求中查询嵌套参数?

List Contacts接口的filter与order_by参数传参方法

一、order_by参数的使用

order_by参数要求传入字段名+方向的组合字符串,行业通用格式是用冒号分隔字段与排序方向,例如字段名:方向。

示例代码:

// 按创建日期倒序获取联系人
fetch("https://api/crm/rest/v2/contacts?page_size=10&order_by=date_created:desc")
  .then(async res => {
    console.log(await res.json());
  });

// 按邮箱正序获取联系人
fetch("https://api/crm/rest/v2/contacts?page_size=10&order_by=email:asc")
  .then(async res => {
    console.log(await res.json());
  });

二、filter参数的使用

filter参数为string类型,REST API中常见两种传参方式,优先推荐JSON字符串编码的方式(支持contact_ids这类数组复杂条件):

1. JSON字符串编码(推荐)

把过滤条件封装成对象,序列化为JSON字符串后,通过encodeURIComponent做URL编码再传入:

单个过滤条件示例:

// 筛选指定邮箱的联系人
const filter = JSON.stringify({ email: "test@example.com" });
fetch(`https://api/crm/rest/v2/contacts?page_size=10&filter=${encodeURIComponent(filter)}`)
  .then(async res => {
    console.log(await res.json());
  });

多条件组合示例:

// 筛选名字为John且所属公司ID为123的联系人
const filter = JSON.stringify({ given_name: "John", company_id: "123" });
fetch(`https://api/crm/rest/v2/contacts?page_size=10&filter=${encodeURIComponent(filter)}`)
  .then(async res => {
    console.log(await res.json());
  });

数组类型条件示例(如contact_ids):

// 筛选指定ID列表的联系人
const filter = JSON.stringify({ contact_ids: ["contact_001", "contact_002"] });
fetch(`https://api/crm/rest/v2/contacts?page_size=10&filter=${encodeURIComponent(filter)}`)
  .then(async res => {
    console.log(await res.json());
  });

2. 键值对拼接格式(部分API支持)

部分API支持用分号分隔多个键值对,格式为字段1:值1;字段2:值2:

// 筛选名字为John的联系人
fetch("https://api/crm/rest/v2/contacts?page_size=10&filter=given_name:John")
  .then(async res => {
    console.log(await res.json());
  });

如果JSON格式尝试后不生效,可以测试这种简单拼接方式。


内容的提问来源于stack exchange,提问作者Pete

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 18:10:32