# Jira API - Add Watcher

**URL:** <https://forums.powershell.org/t/jira-api-add-watcher/23770>\
**Category:** PowerShell Help\
**Created:** [March 13, 2024, 7:53am UTC](https://forums.powershell.org/t/jira-api-add-watcher/23770 "2024-03-13T07:53:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksl28](https://avatars.discourse-cdn.com/v4/letter/k/e19b73/32.png) [@ksl28](https://forums.powershell.org/u/ksl28)\
**Post date:** [March 13, 2024, 7:53am UTC](https://forums.powershell.org/t/jira-api-add-watcher/23770/1 "2024-03-13T07:53:30Z")

</div>

Hello 🙂

I am trying to utilize the Jira Cloud API to add an watcher to an issue.  
[The Jira Cloud platform REST API (atlassian.com)](https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issue-watchers/#api-rest-api-2-issue-issueidorkey-watchers-post)

If i execute this code it add the user that are executing the script as an watcher - just as designed.

```auto

$pair = "$($userName):$($userPassword)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
    Authorization = $basicAuthValue
    "Accept" = "application/json"
    "Content-Type" = "application/json"
}

Invoke-RestMethod -Uri "https://company.atlassian.net/rest/api/2/issue/Key-Number/watchers" -Headers $Headers -Method POST -ContentType "application/json"

```

But if i add an Body then it fails with an Error 400 - this is the code:

```auto

$pair = "$($userName):$($userPassword)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
    Authorization = $basicAuthValue
    "Accept" = "application/json"
    "Content-Type" = "application/json"
}

$Body = @{
    accountId = '123-456-789' #This part is redacted
} | ConvertTo-Json
Invoke-RestMethod -Uri "https://company.atlassian.net/rest/api/2/issue/Key-Number/watchers" -Headers $Headers -Method POST -ContentType "application/json" -Body $Body

```

If i access the API URL in the browser, this is the output:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/7/7c73a4c1cd6af370fc3cc982a31f7f288cfb5a4d.png)

I am sure that my body is incorrect - but i cant figure out why.

Could you help?

---

<div class="post-metadata">

**Author:** ![ksl28](https://avatars.discourse-cdn.com/v4/letter/k/e19b73/32.png) [@ksl28](https://forums.powershell.org/u/ksl28)\
**Post date:** [March 18, 2024, 8:22am UTC](https://forums.powershell.org/t/jira-api-add-watcher/23770/2 "2024-03-18T08:22:18Z")

</div>

Finally found a solution - the body needs to be a string.  
So this worked:

```auto
$Body = """123-456-789"""
Invoke-RestMethod -Uri "https://company.atlassian.net/rest/api/2/issue/Key-Number/watchers" -Headers $Headers -Method POST -ContentType "application/json" -Body $Body

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [March 18, 2024, 7:34pm UTC](https://forums.powershell.org/t/jira-api-add-watcher/23770/3 "2024-03-18T19:34:11Z")

</div>

Thanks for sharing.

FWIW - This is less of a PS question and more of a API specific question regarding formatting. In general these forums are more for PS specific questions, so while some of us may attempt to assist, it’s really out of scope of these forums.

` $Body = """123-456-789"""` seems strange not sure if that was a formatting thing but I’d expect only one set of `"` is needed.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [March 18, 2024, 8:49pm UTC](https://forums.powershell.org/t/jira-api-add-watcher/23770/4 "2024-03-18T20:49:41Z")

</div>

Completely agree this is an API question. FWIW if you’re intending to write a lot of API calls in Powershell, grab Postman which is free. It also has a handy dandy code button on the right ‘\</\>’ that will automatically generate code in many many languages including Powershell 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 17, 2024, 8:50pm UTC](https://forums.powershell.org/t/jira-api-add-watcher/23770/5 "2024-04-17T20:50:13Z")

</div>

Closed on April 17, 2024, 8:50pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
