MS Dynamics REST API FetchXML with a paging cookie
<p>I am using this approach available at <a href="https://msdn.microsoft.com/en-us/library/gg309717.aspx" rel="nofollow">MSDN</a> to retrieve more than 5000 records. I am using MS Dynamics REST API together with fetchXML. My first request looks like this:</p>

<pre><code>//ROOT.dynamics.com//api/data/v8.1/contacts?fetchXml=&lt;fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"&gt;&lt;entity name="contact"&gt;&lt;attribute name="emailaddress1" /&gt;&lt;/entity&gt;&lt;/fetch&gt;
</code></pre>

<p>as a result I got set of records and page cookie for next page:</p>

<pre><code>"@Microsoft.Dynamics.CRM.fetchxmlpagingcookie":"&lt;cookie pagenumber=\"2\" pagingcookie=\"%253ccookie%2520page%253d%25221%2522%253e%253ccontactid%2520last%253d%2522%257bAF17816D-3AAE-E511-80FC-1458D043A570%257d%2522%2520first%253d%2522%257bFF672EF5-22AE-E511-80FC-1458D043A570%257d%2522%2520%252f%253e%253c%252fcookie%253e\" istracking=\"False\" /&gt;"
</code></pre>

<p>I am using this cookie in next request: </p>

<pre><code>//ROOT.dynamics.com//api/data/v8.1/contacts?fetchXml=&lt;fetch mapping='logical' paging-cookie='&amp;lt;cookie page="1"&amp;gt;&amp;lt;contactid last="{UUID}" first="UUID}"/&amp;gt;&amp;lt;/cookie&amp;gt;' page='2'&gt;&lt;entity name='contact'&gt;&lt;all-attributes/&gt;&lt;/entity&gt;&lt;/fetch&gt;
</code></pre>

<p>and got </p>

<pre><code>{
Message: "An item with the same key has already been added.",
ExceptionMessage: "An item with the same key has already been added.",
ExceptionType: "System.ArgumentException",
StackTrace: " at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector) at Microsoft.Crm.Extensibility.OData.CrmODataHeaders.ProcessVersioningQueryParameters() at Microsoft.Crm.Extensibility.OData.CrmODataHeaders..ctor(HttpRequestMessage request) at Microsoft.Crm.Extensibility.OData.Extensions.EdmExtensions.GetCrmODataRequestHeader(HttpRequestMessage request) at Microsoft.Crm.Extensibility.OData.CrmODataRoutingConvention.SelectController(ODataPath odataPath, HttpRequestMessage request) at System.Web.OData.Routing.ODataPathRouteConstraint.SelectControllerName(ODataPath path, HttpRequestMessage request) at System.Web.OData.Routing.ODataPathRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, String parameterName, IDictionary`2 values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.ProcessConstraint(HttpRequestMessage request, Object constraint, String parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.ProcessConstraints(HttpRequestMessage request, HttpRouteValueDictionary values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)",
ErrorCode: 500
}
</code></pre>

<p>This fetchXML works well inside of this <a href="http://msxrmtools.com/fetchxml" rel="nofollow">tool</a>  but not for pure REST API request.
Maybe I have some issue with encode or structure of URL.</p>