【问题标题】:JSON:API specification filter parameters with Flurl HTTPJSON:API 规范过滤参数与 Flurl HTTP
【发布时间】:2021-02-11 01:12:40
【问题描述】:

我们在 API 上使用 JSON:API 规范。目前在 C# 中使用 Flurl 将过滤器参数格式化为规范存在问题。

例子:

        var url = await Helpers.GetAPIPath()
           .AppendPathSegment("orders")
           .WithOAuthBearerToken(Helpers.GetAPIToken())
           .SetQueryParams(new {
               filters = "[work_orders]=true,[status]=pending_approval",
               include = "shipping-address,inventory-items.part"
           }).GetAsync();

这会产生/orders?filters[work_orders]=true,[status]=pending_approval&include=shipping-address,inventory-items.part

这里是关于 JSON:API 请求的文档,带有多个过滤器https://jsonapi.org/recommendations/#filtering

如何为 JSON:API 规范构建类似于 filter[work_orders]=true,filter[status]=pending_approval 的过滤器?

非常感谢任何帮助!

【问题讨论】:

  • 正确的链接是?filter[work_orders]=true&filter[status]=pending_approval。使用& 分隔查询参数而不是,

标签: c# json-api flurl


【解决方案1】:

这个sn-p能够实现正确的过滤参数。不确定是否有更清洁的方法来实现这一点,但到目前为止它正在工作!

         var orderResponse = await Helpers.GetAPIPath()
            .AppendPathSegment("orders")
            .WithOAuthBearerToken(Helpers.GetAPIToken())
            .SetQueryParam("filter[work_orders]=true")
            .SetQueryParam("filter[status]=pending_approval")
            .SetQueryParams(new {
                include = "shipping-address,inventory-items.part",
            }).GetAsync();

【讨论】:

  • 是的,这行得通。对象语法的问题是 [] 字符在 C# 属性名称中是不合法的。 Flurl 将在此处接受字典对象(带有字符串键),或者您在此处完成的方式也可以,但您通常需要将名称和值分开,即 SetQueryParam("filter[work_orders]", "true")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
相关资源
最近更新 更多