Piyush
Piyush

Reputation: 409

Error while creating Work Item in Azure Devops using Rest API

I am getting below error when I am trying to create a Work Item in Azure Devops using the Rest API.

This is the PowerShell script I am using

$body ='[{"op": "add", "path": "/fields/System.Title", "from": null, "value": "Initiative Code"}]' | ConvertTo-Json
$body  = $body | ConvertFrom-Json
$supportAreaUri = 'https://MyOrganization.visualstudio.com/TestTeam1/_apis/wit/workitems/$Initiative?api-version=6.0'
$supportAreaUri = [uri]::EscapeUriString($supportAreaUri) 
$supportArea = Invoke-RestMethod -Method Post -Uri $supportAreaUri -Headers $Header -ContentType application/json -Body $body

I am getting this error when running the code:

"$id": "1",
"innerException": null,
"message": "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.",
"typeName": "Microsoft.VisualStudio.Services.WebApi.VssRequestContentTypeNotSupportedException, Microsoft.VisualStudio.Services.WebApi",
"typeKey": "VssRequestContentTypeNotSupportedException",
"errorCode": 0,
"eventId": 3000

I am getting same error when I try in Postman as Post request.

Upvotes: 0

Views: 3146

Answers (2)

Vito Liu
Vito Liu

Reputation: 8298

Agree with Satya V and Daniel.

Postman result:

enter image description here

Power shell script

$connectionToken="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$Url = "https://dev.azure.com/{Org name}/{Project name}/_apis/wit/workitems/"+"$"+"{Work item type}?api-version=6.0"  
$Body = @"
[
 {
 "op": "add",
 "path": "/fields/System.Title",
 "from": null,
 "value": "Sample Bug Test"
 }
]
"@
$Result = Invoke-RestMethod -Uri $Url -ContentType "application/json-patch+json" -Body $Body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
write-host $Result.id

Result:

enter image description here

Upvotes: 1

Satya V
Satya V

Reputation: 4164

Looking through the error message, appears like you are passing the Content Type : "application/json"

You will have to pass the Content Type header as application/json-patch+json

The corrected command would be

Invoke-RestMethod -Method Post -Uri $supportAreaUri -Headers $Header -ContentType "application/json-patch+json" -Body $body

enter image description here

Reference

Upvotes: 3

Related Questions