Alan Judi
Alan Judi

Reputation: 1102

Server Returns Bad Request 400 On REST POST CALL, even though uri is correct

I am trying to add an option label and option value to an optionset field(new_contractserving) found on an entity called new_servingtime. Not sure if I am doing this correctly, but the server throws a 400 Bad request, what's the issue?!

var entity = {

                "new_contractserving": String(OptionValue),
                "[email protected]": String(OptionText)
            };

            var reqJSON = new XMLHttpRequest();
            reqJSON.open("POST", url + "/api/data/v8.2/new_servingtimes", false);
            reqJSON.setRequestHeader("OData-MaxVersion", "4.0");
            reqJSON.setRequestHeader("OData-Version", "4.0");
            reqJSON.setRequestHeader("Accept", "application/json");
            reqJSON.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            reqJSON.onreadystatechange = function () {
                if (this.readyState === 4) {
                    reqJSON.onreadystatechange = null;
                    if (this.status === 204) {
                        var uri = this.getResponseHeader("OData-EntityId");
                        var regExp = /\(([^)]+)\)/;
                        var matches = regExp.exec(uri);
                        var newEntityId = matches[1];
                    } else {
                        Xrm.Utility.alertDialog(this.statusText + ": Third Request!");
                        return;
                    }
                }
            };
            reqJSON.send(entity);

Upvotes: 2

Views: 1688

Answers (2)

You should be using InsertOptionValue Action to add new option to the existing picklist attribute in an entity.

CRM REST Builder is the best choice to compose such requests & test.

The request you have written can be used to set attribute value in a record, but still it’s incomplete. Read this blog to understand how you can execute webapi action.

Upvotes: 2

so-random-dude
so-random-dude

Reputation: 16505

HTTP 400 means bad data. If it was "URI not found" it would have been a HTTP 404

HTTP 400 on a POST usually means, your request (requestbody) failed some validation on the server side or it did not confine to the format which server is expecting

Upvotes: 3

Related Questions