2

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',
Jigar Parekh
  • 595
  • 10
  • 23
  • 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](https://learn.microsoft.com/en-us/rest/api/azure/devops/build/folders/create?view=azure-devops-rest-4.1) and an [Update call with a POST](https://learn.microsoft.com/en-us/rest/api/azure/devops/build/folders/update?view=azure-devops-rest-4.1). I think you can do the following in general: Create = POST and Update = PUT – R Pelzer Dec 04 '19 at 15:42
  • I have tried same thing with the post method but it is not updating. – Jigar Parekh Dec 04 '19 at 15:43
  • 1
    Can you post the body you're sending in your PATCH? – Josh Gust Dec 04 '19 at 18:10
  • @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. – Mengdi Liang Dec 05 '19 at 12:38

1 Answers1

2

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

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • This is working for me. But after updating status it is giving an error in ajax "No conversion from text to application/json-patch+json" – Jigar Parekh Dec 06 '19 at 16:28
  • @JigarParekh This should as expected. It represent did not get anything from server. Do you use the fiddler to capture this post? If yes, you would see that there’s nothing in the response body. Because server just get the post request and execute it, but nothing reply to client. – Mengdi Liang Dec 06 '19 at 16:39
  • @JigarParekh, See my updated pic in the answer. You can see when you call the api with ajax, there's no response message replied from server(**But the operation is succeed**). As I checked from google, this seems the **default issue** of JQuery: https://stackoverflow.com/questions/10456240/jquery-ajax-call-return-json-parsing-error. Not relevant with Azure Devops. – Mengdi Liang Dec 08 '19 at 11:40