Update customfield in JIRA using REST API without CURL

658 Views Asked by At

I am new to working with PowerShell and trying to use JIRA's Rest API (without cURL-command) to update certain custom field like "Description". But I can not get the right way. Below is my code:

        $body = {"update": 
        {"customfield_17526":  
            [        
                {           
                    "set": 
                    [            
                        { "description": "trying to use JIRA's Rest API to update custom field" }          
                    ]        
                }      
            ]    
        }  
    }
Invoke-RestMethod -uri $restapiuri -Headers $Headers -Method PUT -ContentType 
"application/json" -Body $body

What is wrong in the code above?

Thanks and regards

2

There are 2 best solutions below

0
Kyrellos On BEST ANSWER

The problem was actually in the JSON format. In my case the parameter '$body' must be edited like following:

$body = @{
fields = @{
    project = @{
        key = "TEST"
    }
    summary = "Test summary
    description = "Test description"
    issuetype = @{
            id = "123"
        }
    }

}

I came to this solution after multiple tries.

It is also very important to follow jira rules according the order of the fields, otherwise you get a 'bad request 400'.

Generally it is recommended to see some examples, as CraZ mentioned in his links or in this one: https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/

6
CraZ On

In this case, Description is system field and you seem to update a custom field (with ID 17526).

If you mean the Description system field, then use this JSON:

{
  "update": {
    "summary": [{
        "set" : "New description"
        }]
  }
}

If you really mean a Description custom field (that one with ID 17526), then update it this way:

{
  "fields": {
    "customfield_17526": [{
        "set" : "Some text"
        }]
  }
}

It's wise to use official Atlassian documentation:

Also it's worth installing REST API Browser add-on onto your instance.