Working with the Azure DevOps API: Headers are important!

Working with the Azure DevOps API: Headers are important!

While working with the APIs one might stumble across challenges that can cost lots of time and sometimes it is even difficult to find the right term to search for an answer. The API of Azure DevOps is no exception here. One of the easier things is the Content/Media Type while using the Azure DevOps API. In some of the API calls the Content Type ‘application/json’ has to be used in the header, in other calls ‘application/json-patch+json’ has to be used.

Why is this even a problem?

When I think of PATCH I expect there is already a resource that should be changed/updated. The documentation of the HTTP request methods states “The HTTP PATCH request method applies partial modifications to a resource.“ In the Azure DevOps API ‘Content-Type’ => json-patch+json’ is sometimes also required when a new resource is created for example when Creating a new Workitem see Azure DevOps docs.

What happens if the wring header is used?

Pretty simple, you get a 400 Bad Request response. The good thing is that in the body the reason for the exception is clearly stated: “The request indicated a Content-Type of "application/json" for method type "POST" which is not supported. Valid content types for this method are: application/json-patch+json.”

How can this be avoided?

Also pretty simple – like in so many other cases reading the documentation helps! If ‘Content-Type’ => ‘application/json-patch+json’ is expected, it is stated in the capter “Request Body” of the documentation.

image Source: https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work-items/create?view=azure-devops-rest-6.0#request-body

What are are obstacle you tripped over? Let me know your via mail: stefan.marjanko@aveyara.com

Special credits to Max Pixel for the headline picture.