首页 > 解决方案 > JSON:API 规范过滤参数与 Flurl HTTP

问题描述

我们在 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

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

任何帮助深表感谢!

标签: c#json-apiflurl

解决方案


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

         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();

推荐阅读