Search code examples
javascriptajaxazure-devopsazure-devops-rest-apihttp-patch

devops REST API: can not update work item status using PATCH method


I am using DevOps restapi to get some information. The POST method is working fine for me.
I want to update the status of my work item. For that I need to use the PATCH method. Which is not working, and not giving any kind of error.

https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/update?view=azure-devops-rest-5.1

function postApiData(ApiUrl, responseBody) {
    var res = '';
    try {

        $.ajax({
            type: 'POST',
            async: false,
            url: ApiUrl,
            contentType: 'application/json',
            data: JSON.stringify(responseBody),
            cache: false,
            dataType: 'json',
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", "Basic " + btoa("" + ":" + _token));
            },
        }).done(function (data) {
            res = data;
        }).fail(function (e) {

        });
    } catch (error) {
        var x = error;
        throw x;
    }
    return res;
};

For Patch method, I am modifying a few things. but it is not giving any error not updating my work item. I have also checked my token access. I have full access.

type: 'PATCH',
contentType: 'application/json-patch+json',

Solution

  • I wrote a simple sample on my side with PATCH in Ajax:

    <script type="text/javascript">
    $(document).ready(function () {
        $("#SelectWIT").on("click", function () {
            var json= [{
                    "op": "add",
                    "path": "/fields/System.State",
                    "value": "Closed"
                  }];
            $.ajax({
                type: 'PATCH',
                url: 'https://dev.azure.com/{org name}/_apis/wit/workitems/{WIT id}?api-version=5.1',
                contentType: 'application/json-patch+json',
                data: JSON.stringify(json),
                cache: false,
                dataType: 'application/json-patch+json',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic " + btoa("" + ":" + "{PAT token}"));
                },
            }).error(function (e) {
                var s = "error error error";
            });
        })
    });
    </script>
    

    Note: Not only contentType need to set as application/json-patch+json, but also same in dataType.


    I use Fiddler to catch this operation:

    enter image description here

    You can see the work item status updated successfully.

    UPDATE:

    enter image description here