2
votes

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://docs.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',
1
I Guess they mixed up a lot regarding the method in the Azure DevOps documentation. Here you also see a create call with a PUT and an Update call with a POST. I think you can do the following in general: Create = POST and Update = PUTR Pelzer
I have tried same thing with the post method but it is not updating.Jigar Parekh
Can you post the body you're sending in your PATCH?Josh Gust
@JigarParekh, you can use Fiddler to capture the error. It can show you why it didn't update work item successfully. Also, with the help of Fiddler, I wrote a simple sample which is work succeed now. You can try with it.Merlin Liang

1 Answers

2
votes

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