Q: Dynamics CRM web api 8.2 'Malformed XML in the Paging Cookie' I use Web Resource JavaScript file to retrieve multiple records from CRM.
var fetchXML = `
            <fetch mapping="logical" output-format="xml-platform" version="1.0" page="1">
              <entity name="account" >
                <attribute name="name" />
              </entity>
            </fetch>`;

var query = "accounts?fetchXml=" + fetchXML;

callWebAPI(query);

After I get paging-cookie in first request I try to send it to the second request to retrieve data for second page:
<fetch mapping="logical" output-format="xml-platform" version="1.0" page="2" paging-cookie="cookie i get from first request"
     ...
</fetch>`;

Original cookie from response looks like:
%253ccookie%2520page%253d%25221%2522%253e%253cname%2520last%253d%2522Deco%2520Voyages%2522%2520firstnull%253d%25221%2522%2520%252f%253e%253caccountid%2520last%253d%2522%257b9AFBEAA6-9EA7-E711-8103-70106FAA4841%257d%2522%2520first%253d%2522%257b0A86656D-BEA7-E711-8103-70106FAA4841%257d%2522%2520%252f%253e%253c%252fcookie%253e

I tried to transform and send cookie according to documentation: https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/org-service/page-large-result-sets-with-fetchxml
var transformedCookie1 = GetDecodedCookie1(decodeURIComponent(decodeURIComponent(pagingcookie)));

var transformedCookie2 = GetDecodedCookie2(decodeURIComponent(decodeURIComponent(pagingcookie)));

function GetDecodedCookie1(cookie) {
    return cookie.replace(/</g, "&lt;")
                 .replace(/>/g, "&gt;")
                 .replace(/"/g, "&quot;")
}

function GetDecodedCookie2(cookie) {
    return cookie.replace(/</g, "%26lt;")
                 .replace(/>/g, "%26gt;")
                 .replace(/"/g, "%26quot;")
}

1) In first case when I use GetDecodedCookie1 I get:
Script error. in  at 0:0  null


My Query String Parameters are broken. 
2) In second case when I use GetDecodedCookie1 Query String Parameters looks fine but I get:
Malformed XML in the Paging Cookie

What is the issue here ?

A: the Paging Cookie needs the id of the last record, include the "accountid" as an attribute to your fetchxml
Hope it Helps - M.Acosta.D

A: Is the use of FetchXML mandatory to query the Web API? Otherwise, you can simplify your life by using OData. Your FetchXML translated to an OData query is:
https://[Organization URI]/api/data/v8.2/accounts?$select=name

Which would return a response similar to the one below IF there would be more than 5000 accounts in Dynamics, otherwise it would not contain a @odata.nextLink attribute (I've collapsed value collection for better readability):
{
     "@odata.context": "https://[Organization URI]/api/data/v8.2/$metadata#accounts(name)",
     "value": [],
     "@odata.nextLink": "https://[Organization URI]/api/data/v8.2/accounts?$select=name&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%253e%253caccountid%2520last%253d%2522%257b92655027-054C-E911-A817-000D3ABA3F3F%257d%2522%2520first%253d%2522%257b93C71621-BD9F-E711-8122-000D3A2BA2EA%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20/%3E"
}

After retrieving this response you just have to parse it and do a request to the link given by @odata.nextLink attribute to retrieve the next batch of records, in my case this link would retrieve account 5001 to 10000.
For more information on how to Query Data using the Web API click here for the official documentation.
