Forums

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

404 Error on Rest API Call

Jeret Shuck
Contributor
June 23, 2020

I've read just about ever bit of documentation I can find however I'm unable to get this to work. I'm using powershell to attempt a REST-API GET method;

# Create username / API key pair, then encode it
$baseString = "first.last@mydomain.com:<myAPIKey>"
$baseBytes = [System.Text.Encoding]::Unicode.GetBytes($baseString)
$base64 = [System.Convert]::ToBase64String($baseBytes)

# Build headers
$authHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$authHeaders.Add('Basic', ('Basic {0}' -f $base64))
$authHeaders.Add('Accept', 'application/json')

# Call Rest API
Invoke-RestMethod -Uri "https://mydomain.atlassian.net/rest/api/3/issue/TS-1000" -Method Get -Headers $authHeaders

 

Each time I'm getting a "Invoke-RestMethod : The remote server returned an error: (404) Not Found." error.

 

URL works without issue or a 404 error in a browser as seen:

image.png

1 answer

1 accepted

1 vote
Answer accepted
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 25, 2020

Hi Jeret,

I see that you're using Powershell in order to try to make a REST API call to a Jira Cloud site, but that this is not working as expected.  I took a closer look at your steps to make this work and I believe I see the problem here.

You are using the base64 encoded string of emailaddress:API_Token, however the way you encode that string is technically different from the way we state to encode it in our Basic Auth for REST APIs documentation.

To highlight this difference I used an example string of 'first.last@mydomain.com:<myAPIKey>' and in the first example I used the documented method, and the second I used your method.  In order for this to work correctly I'd expect the same output

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\IEUser> $Text = `first.last@mydomain.com:<myAPIKey>'
PS C:\Users\IEUser> $Bytes = [System.Text.Encoding]::UTF8.GetBytes($Text)
PS C:\Users\IEUser> $EncodedText = [Convert]::ToBase64String($Bytes)
PS C:\Users\IEUser> $EncodedText
Zmlyc3QubGFzdEBteWRvbWFpbi5jb206PG15QVBJS2V5Pg==
PS C:\Users\IEUser>
PS C:\Users\IEUser>
PS C:\Users\IEUser> $baseString = "first.last@mydomain.com:<myAPIKey>"
PS C:\Users\IEUser> $baseBytes = [System.Text.Encoding]::Unicode.GetBytes($baseString)
PS C:\Users\IEUser> $base64 = [System.Convert]::ToBase64String($baseBytes)
PS C:\Users\IEUser> $base64
ZgBpAHIAcwB0AC4AbABhAHMAdABAAG0AeQBkAG8AbQBhAGkAbgAuAGMAbwBtADoAPABtAHkAQQBQAEkASwBlAHkAPgA=

But as you can see, your example is encoding this string much differently.   I suspect this might be due to the use of the unicode.getbytes instead of the UTF8 we are expecting here.  I also tried with double quotes instead of single, but that doesn't seem to make a difference.

Try instead to use this method for encoding your credentials:

$Text = ‘user@example.com:api_token_string’
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($Text)
$EncodedText = [Convert]::ToBase64String($Bytes)
$EncodedText

The use of the incorrect credentials here would certainly be a plausible explanation for the 404 error you see here.  Try that and let me know the results.

Andy

Jeret Shuck
Contributor
June 26, 2020

Andy, thanks for taking the time to write and test with such a detailed response. I really appreciate it.

I also found another thread that used `UTF8` instead of Unicode. The interesting thing is I ran a `Compare-Object` on both of those outputs and saw no difference. None the less, I did get things working with the `UTF8` method. 

 

Odd, but glad I got it working. 

sriprasanna.mg
Contributor
October 15, 2020

We are facing the same issue as well (404) Not Found.

We already switched the encoding to UTF8 but the issue still persist.

One difference I noticed is in our case we use username and password instead of API token.

$basicAuth = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($JiraUser):$JiraPwd"))  
      
$headers = @{"Authorization" = $basicAuth
"Content-Type"  = "application/json"}
sriprasanna.mg
Contributor
October 15, 2020

Sorry, My bad! 

The rest api url had a mistake:, there was an additional /jira included.

May be I just copy pasted from an example and did not really validate the ling

https://jira.test.com/jira/rest/api/latest/issue/TVM-xx/remotelink

https://jira.test.com/rest/api/latest/issue/TVM-xx/remotelink

Suggest an answer

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

Atlassian Community Events