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

时间:2019-05-13 20:56:25

标签: rest microsoft-dynamics

我们正在使用REST API从Microsoft Dynamics系统中提取联系人。有大约。 88K联系人,我们希望通过使用检查“ modifiedon”字段的过滤器来减少传输的数据量。将过滤器添加到查询后,它会返回400响应。我们尝试使用多个字段进行过滤,尝试使用单个刻度来过滤值,并且尝试使用双击来包装值。

没有$ filter参数的请求

此查询不包含$ filter参数,并将返回5K的88K联系人。

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

这将返回5K结果,并带有下一个链接,使我们可以迭代数据库中的所有结果。

回复

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

带有$ filter参数的请求

此查询确实包含$ filter参数。运行此命令时,动力学将返回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条响应

注意:以下尝试也会返回400个错误:

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

https://[HOST].api.crm.dynamics.com/api/data/v9.0/contacts?$ select =电子邮件地址1,全名,昵称,电话1,创建,修改日期,_parentcustomerid_value&$ filter =修改日期>“ 2018-01-01”

https://[HOST].api.crm.dynamics.com/api/data/v9.0/contacts?$ select =电子邮件地址1,全名,昵称,电话1,创建,修改,_parentcustomerid_value&$ filter =全名为空

2 个答案:

答案 0 :(得分:0)

我创建了一个Trial Instance,并使用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 Docs中提供了5k记录的说明

  

指定要在页面中返回的实体数   odata.maxpagesize首选项值,用于请求实体数   在响应中返回。

     

注意

     

您不能使用大于5000的odata.maxpagesize首选项值。

    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"
    }

答案 1 :(得分:0)

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

这有效!

  

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

这不起作用。

  

$ filter = modified on gt'2019-05-07'