Common Error – Bad request when using Web API CRM 2016

5 comments

Hi,

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 = Xrm.Page.data.entity.getId();
var entity = {};
entity.statecode = 1;
entity.statuscode = 2;

var req = new XMLHttpRequest();
req.open(“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 {
alert(this.statusText);
}
}
};
req.send(JSON.stringify(entity));

When i execute it returned me saying

sign-bad-request-computer-35718948.jpg

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

 

 

Advertisements

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.