Common Error – Bad request when using Web API CRM 2016



I have come across an error while using web API, I was trying to retrieve and update values from one of entities.


Below is the API request.

var id =;
var entity = {};
entity.statecode = 1;
entity.statuscode = 2;

var req = new XMLHttpRequest();“PATCH”, Xrm.Page.context.getClientUrl() + “/api/data/v8.0/quotes(” + id + “)”, true);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
//Success – No Return Data – Do Something
else {

When i execute it returned me saying


Then i have looked into some articles about Web API in web , which did not helped me.

After spending some time, I realized that when i take current entity id it comes with {}(flower brackets) and the same is being passed to API which in turn becomes a bad request.

Then i have replace {} with space then it worked like charm.

 id = id.replace(/\{|\}/gi, ”);

This looks like a small issue but i am damn sure it will waste your time.

Hope it helps.

Thank you,

Sreeni Pavalla



5 comments on “Common Error – Bad request when using Web API CRM 2016”

  1. Hi Sreeni, thanx for your post, but the resolution you posted is missing singe quotes:
    id = id.replace(/\{|\}/gi, ”);
    should be
    id = id.replace(‘/\{|\}/gi, ”’);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.