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',
4
  • 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 = PUT Commented Dec 4, 2019 at 15:42
  • I have tried same thing with the post method but it is not updating. Commented Dec 4, 2019 at 15:43
  • 1
    Can you post the body you're sending in your PATCH? Commented Dec 4, 2019 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. Commented Dec 5, 2019 at 12:38

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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"
@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.
@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: stackoverflow.com/questions/10456240/…. Not relevant with Azure Devops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.