Common Error – Bad request when using Web API CRM 2016

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

 

 

Advertisement

Published by Sreeni Pavalla

Working as a Assistant Technical Manager in Positive Edge Technologies. Having over 11 years of extensive hands-on experience in MS Dynamics CRM 4.0, 2011, 2013, 2015, 2016, Dynamics 365 with over 5 years of experience in CRM Online. You can visit my blog here https://sreenipavalla.me/

5 thoughts 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, ”’);

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: