Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Answered

API query with Odata date filter with encoding

Posted on by Microsoft Employee

Hi

When calling an API endpoint on api.businesscentral.dynamics.com from BizTalk, we are required to include what looks like an Odata filter on the uri:

e.g:

&$filter=(documentDate ge 2020-07-01) and (documentDate le 2020-07-31)

In PostMan this returns records, but when the spaces are "encoded" it accepts the request but returns no records

e.g:

&$filter=(documentDate%20ge%202020-07-01)%20and%20(documentDate%20le%202020-07-31)

or

&$filter=(documentDate+ge+2020-07-01)+and+(documentDate+le+2020-07-31)

At the moment its a real struggle to change the BizTalk send behaviour to stop the encoding, so I was wondering if this is a API config issue?

Regards

  • steveendow Profile Picture
    steveendow 2,281 on at
    RE: API query with Odata date filter with encoding

    Excellent!  Thanks for the update!

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: API query with Odata date filter with encoding

    Ok, sorry, a quick update to say that the fully encoded url did in fact yield results.

    This turned out to be an issue with the actual date parameters we were asked to test with!

    The API quite happily returns results if you encode spaces with "%20" or "+" (even within the OData filter).

    Many thanks

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: API query with Odata date filter with encoding

    Thanks Steve for these suggestions.

    What you suggested is what we've already tried.

    The only difference being that we have additional parameters on the call.

    Full example:

    v1.0/salesCreditMemos?$expand=*&company=TEST%20UK%20Ltd.&$filter=(documentDate%20ge%202020-07-01)%20and%20(documentDate%20le%202020-07-31)

  • steveendow Profile Picture
    steveendow 2,281 on at
    RE: API query with Odata date filter with encoding

    Hi Andrew,

    Very interesting question.  I just did some tests and reviewed the OData V4 specifications, and here's my observation.

    It appears that the filter "values" can be encoded, but the OData filter syntax itself cannot be encoded.

    See section 2.2 URL Syntax on this page:

    http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html

    The examples provided are not great, but notice that the parentheses are not encoded--only the filter "values" inside the parentheses are encoded.

    The OData standard appears to have some optional conventions, so I have found that Business Central doesn't support all of the specifications in that documentation, but seems to support most.

    I did a test with the BC Web API, and here's what I found:

    Unencoded filter works properly:

         items?$filter=number eq '1896-S'

    Fully encoded filter does not work (filter ignored):

        items?$filter%3Dnumber%20eq%20%271896-S%27

    Equals sign is NOT encoded, but everything after equals sign is encoded works:

        items?$filter=number%20eq%20%271896-S%27

    Based on this last test, encoding spaces AND apostrophes works for me

    I then tested with date ranges, which also worked:

         purchaseInvoices?$filter=(invoiceDate%20ge%202021-01-01)%20and%20(invoiceDate%20le%202021-02-01)

    This above example appears to be essentially the same as what you are using.

    The one thing I notice is that you have & before your "$filter" keyword.

    Can you try ? before the $filter?  In my example above, I'm using purchaseInvoices?$filter...

    Here's a full URL example:

    https://api.businesscentral.dynamics .com/v2.0 / mycompany.com/ Sandbox1/ api/v2.0/ companies(f11e1111-3d8a-eb11-bb5f-000d3a398a56)/ purchaseInvoices?$filter=(invoiceDate%20ge%202021-01-01)%20and%20(invoiceDate%20le%202021-02-01)

    .

    Let me know if the & vs ? makes a difference.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans