Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Invoke-WebRequest : The remote server returned an error: (415) Unsupported Media Type.

Dennis Nguyen
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 20, 2022

Hello Atlassian team, I am getting the error of call REST API to Atlassian.

 

Below is my code to query a ticket with contains a provided keywords. Then these ticket will be closed by transition ID 21

# Replace YOUR_JIRA_URL, YOUR_USERNAME, and YOUR_API_TOKEN with your own Jira URL, username, and API token
$jiraUrl = "https://xxx.atlassian.net"
$username = "xxx@xxx.com"
$apiToken = "xxxxxxxxxxxxxxxxxxx"

# Replace TICKET_NAME with the name of the ticket you want to close
$ticketName = "Scheduled Tasks"

# Set up the base64-encoded credentials for the Jira API
$authInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$apiToken)))

# Set the user agent to a recognizable value
$headers = @{Authorization=("Basic {0}" -f $authInfo); "User-Agent"="Powershell Script"}


# Get a list of all open tickets with the provided ticket name

$response = Invoke-WebRequest -Uri "$jiraUrl/rest/api/2/search?jql=project%20=%20ITENG%20AND%20summary%20~%20'$ticketName'%20AND%20status%20in%20('In%20Progress',%20'Incident%20Reported',%20New)%20order%20by%20created%20DESC" -Method GET -Headers $headers
$results = (ConvertFrom-Json $response.Content).issues

 

----above code ran well and return all ticket thru $result.-----------

However, I got issue 415 when I ran the REST API below:

 

foreach ($result in $results) {
# Get the ticket key and id
$ticketKey = $result.key
$ticketId = $result.id
# Set the request body for the transition to the closed state
$requestBody = @{
transition = @{
id = "21"
}
} | ConvertTo-Json

# Perform the transition to the closed state
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$transitionResponse = Invoke-WebRequest -Uri "$jiraUrl/rest/api/2/issue/$ticketKey/transitions" -Method POST -Headers $headers -Body $requestBody
Write-Output $transitionResponse
}

 

2 answers

0 votes
WW
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2024

Better late than never, and just in case I forget and run across this again...

You have to have this in the request line:

 -ContentType "application/json"
Even if you have it in the header, it won't work, at least it didn't for me. I don't see you have it anywhere.
Your code would look like:
$transitionResponse = Invoke-WebRequest -Uri "$jiraUrl/rest/api/2/issue/$ticketKey/transitions" -Method POST -Headers $headers -Body $requestBody -ContentType "application/json"
 
As an added tip:
You could use Invoke-RestMethod instead of Invoke-WebRequest if you wanted to. Invoke-RestMethod will automatically return your results as a PSCustomObject, which would allow you to more easily get to the values. You wouldn't have to use the ConvertFrom-Json line. Invoke-RestMethod essentially does what Invoke-WebRequest + ConvertFrom-Json does.
0 votes
Dennis Nguyen
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 20, 2022

FYI: I am running this script with Powershell

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events