0

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
  • Please provide the exact (error) response from the server. Jira should always tell you what's wrong in your request. Commented Oct 7, 2022 at 14:41
  • Here is the error warning: Unexpected token':' in expression or statement. Commented Oct 10, 2022 at 14:38

2 Answers 2

1

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/

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

Comments

0

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.

6 Comments

Thanks for your reply @Creaz. But it does not work, as I am not working with 'curl'. So I have to save the update-statement in a parameter like '$body="{"fields": {... etc"' and then Invoke-RestMethod with a PUT Method. $description = Invoke-RestMethod -uri $restapiuri -Headers $Headers -Method PUT -ContentType "application/json" -Body $body Moreover PS complains about 'unexpected token' in ':'
@Kyrellos, it doesn't matter what tool you use (PS, curl, Python, ...). It's only about the data (body) you provide. According to the error the problem seems to be only in the JSON you provide. I'm not familiar with PS, but according to some sources you should define it as object (i.e. $body = @{ ... and also convert it to JSON using ConvertTo-Json cmdlet.
I made some changes & get other error messages $body = '{ "update": { "customfield_10130": [ { "set": [ { "description": "Test Test" } ] } ] } }' $description = Invoke-RestMethod -uri $restapiuri -Headers $Headers -Method PUT -ContentType "application/json" -Body $body => Error: The remote server returned an error: (400) Bad Request.
Then I tried to add following encoding to my JSON-Object -> ([System.Text.Encoding]::UTF8.GetBytes($JSON)) But I get 'Exception calling "GetBytes" with 1 argument(s): "The array cannot be NULL.'
Sorry, I can't help any further. This seems to be related to PS, not Jira REST API or JSON.
|

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.