0

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.

1
  • 1
    Did you look closely at the error message and the documentation try adjusting your code accordingly? Valid content types for this method are: application/json-patch+json. Commented Dec 8, 2020 at 4:34

2 Answers 2

3

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

0
1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.