Microsoft Dynamics REST API $filter 参数返回 400 错误

Microsoft Dynamics REST API $filter parameter returning 400 error

我们正在使用 REST API 从 Microsoft Dynamics 系统中提取联系人。有大约。 88K 联系人,我们希望通过使用检查 'modifiedon' 字段的过滤器来减少传输的数据量。当我们将过滤器添加到查询时,它是 returning 400 响应。我们尝试过使用多个字段进行过滤,我们尝试使用单勾号的值进行过滤,我们尝试使用双击来包装值。

没有 $filter 参数的请求

此查询包含 $filter 参数,并将 return 88K 个联系人中的 5K 个。

URL: https://[HOST].api.crm.dynamics.com/api/data/v9.0/contacts?$select=emailaddress1,fullname,nickname,telephone1,createdon,modifiedon,_parentcustomerid_value

这将 return 5K 个结果,下一个 link 允许我们迭代数据库中的所有结果。

回应

(
    [@odata.etag] => *************
    [emailaddress1] => *************
    [fullname] => *************
    [nickname] => 
    [telephone1] => 
    [createdon] => 2018-12-18T22:17:23Z
    [modifiedon] => 2018-12-18T22:17:26Z
    [_parentcustomerid_value] => 
    [contactid] => *************
)

使用 $filter 参数请求

此查询包含 $filter 参数。当这是 运行 时,动态 return 出现 400 错误。

URL: https://[HOST].api.crm.dynamics.com/api/data/v9.0/contacts?$select=emailaddress1,fullname,nickname,telephone1,createdon,modifiedon,_parentcustomerid_value&$filter=modifiedon gt 2018-01-01

回应

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request</h2>
<hr><p>HTTP Error 400. The request is badly formed.</p>
</BODY></HTML>

注意:我们也尝试了这些查询并获得了 400 响应

注意:以下尝试也 return 400 错误:

https://[HOST].api.crm.dynamics.com/api/data/v9.0/contacts?$filter=modifiedon gt '2018-01-01'

https://[HOST].api.crm.dynamics.com/api/data/v9.0/contacts?$select=emailaddress1,fullname,nickname,telephone1,createdon,modifiedon,_parentcustomerid_value&$filter=modifiedon gt "2018-01-01"

https://[HOST].api.crm.dynamics.com/api/data/v9.0/contacts?$select=emailaddress1,fullname,nickname,telephone1,createdon,modifiedon,_parentcustomerid_value&$filter=fullname ne null

我创建了一个试用实例并厌倦了使用 Postman 调用 Webapi,我能够过滤记录。

 https://[host].api.crm4.dynamics.com/api/data/v9.1/contacts?$select=fullname&$filter=modifiedon gt '2019-05-13'

    Result

    "value": [
            {
                "@odata.etag": "W/\"1732225\"",
                "fullname": "test contact",
                "contactid": "9fb24443-1476-e911-a975-000d3ab0fc6e"
            }


    https://[host].api.crm4.dynamics.com/api/data/v9.1/contacts?$select=fullname&$filter=fullname ne null

    Result

     "value": [
            {
                "@odata.etag": "W/\"1732225\"",
                "fullname": "test contact",
                "contactid": "9fb24443-1476-e911-a975-000d3ab0fc6e"
            },
            {
                "@odata.etag": "W/\"1445929\"",
                "fullname": "Portal Customer",
                "contactid": "99db51a2-c34e-e111-bb8d-00155d03a715"
            },
            {
                "@odata.etag": "W/\"1445935\"",
                "fullname": "System Administrator",
                "contactid": "7469fd95-c0bd-4236-90bf-1d1100291df5"
            },
            {
                "@odata.etag": "W/\"1668674\"",
                "fullname": "Vincent Lauriant",
                "contactid": "465b158c-541c-e511-80d3-3863bb347ba8"
            },
            {
                "@odata.etag": "W/\"1437642\"",
                "fullname": "Adrian Dumitrascu",
                "contactid": "49a0e5b9-88df-e311-b8e5-6c3be5a8b200"
            },
            {
                "@odata.etag": "W/\"1667039\"",
                "fullname": "Cathan Cook",
                "contactid": "4ba0e5b9-88df-e311-b8e5-6c3be5a8b200"
            },

Microsodt 文档中提供了 5k 条记录的说明

Specify the number of entities to return in a page Use the odata.maxpagesize preference value to request the number of entities returned in the response.

Note

You can’t use an odata.maxpagesize preference value greater than 5000.

    Request
        GET [Organization URI]/api/data/v9.0/accounts?$select=name HTTP/1.1  
        Accept: application/json  
        OData-MaxVersion: 4.0  
        OData-Version: 4.0  
        Prefer: odata.maxpagesize=3 

Response
    HTTP/1.1 200 OK  
    Content-Type: application/json; odata.metadata=minimal  
    OData-Version: 4.0  
    Content-Length: 402  
    Preference-Applied: odata.maxpagesize=3  

    {  
       "@odata.context":"[Organization URI]/api/data/v9.0/$metadata#accounts(name)",
       "value":[  
          {  
             "@odata.etag":"W/\"437194\"",
             "name":"Fourth Coffee (sample)",
             "accountid":"7d51925c-cde2-e411-80db-00155d2a68cb"
          },
          {  
             "@odata.etag":"W/\"437195\"",
             "name":"Litware, Inc. (sample)",
             "accountid":"7f51925c-cde2-e411-80db-00155d2a68cb"
          },
          {  
             "@odata.etag":"W/\"468026\"",
             "name":"Adventure Works (sample)",
             "accountid":"8151925c-cde2-e411-80db-00155d2a68cb"
          }
       ],
       "@odata.nextLink":"[Organization URI]/api/data/v9.0/accounts?$select=name&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%253e%253caccountid%2520last%253d%2522%257b8151925C-CDE2-E411-80DB-00155D2A68CB%257d%2522%2520first%253d%2522%257b7D51925C-CDE2-E411-80DB-00155D2A68CB%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20/%3E"
    }

我发现了问题,归结为查询中的空格 PHP 和 cURL 未编码。感谢 AnkUser 建议使用不同的工具进行测试。

这有效!

$filter=modifiedon%20gt%20'2019-05-07'

这行不通。

$filter=modifiedon gt '2019-05-07'