0

Could you help me?

I am trying to create an issue in Jira using the Powershell Invoke-WebRequest cmdlet. And I am getting 400 Bad Request error.

I was able to send a successful request using Postman, so I made sure the body syntax is correct and I have sufficient rights.

My code:

$body = @{ 
    "fields" = @{
            "project"=
            @{
                "key"= "ProjectKey"
            }
    
            "summary"= "Test"
    
            "description"= "Test"
            "issuetype" =@{
                "id"= "10705"
    
            }
            "priority"= @{
                "id"= "18"
            }
            "reporter"= @{"name"= "MyName"}
            
    }
    
    }
    
    
    $Headers =  @{
    Authorization = "Basic QWxla0Zblablablablablablabla" #I took it from Postman
    }
    
    $restapiuri = "https://jira.domain.com/rest/api/2/issue"
    
    Invoke-RestMethod -Uri $restapiuri -ContentType "application/json"  -Body $body -Method POST -Headers $Headers

for example, I can successfully execute

Invoke-RestMethod "https://jira.domain.com/rest/api/2/issue/createmeta" -Headers $Headers 

I've already spent a lot of time-solving this problem, but still can't create an issue.

Any help, please

  • 1
    I am by no means an expert in this but I think you need to `ConvertTo-Json` your `$body` before passing it to `Invoke-RestMethod`. – Rno Apr 10 '21 at 21:55
  • Rno is correct. The body must be in JSON format before submission. – David Bakkers Apr 11 '21 at 00:14
  • I don't know if it would be right to delete my comment so as not to mislead people. ConvertTo-Json really helped. But it didn’t work the first time, as there were other errors that I didn’t know about, and the error remained the same, for example, I assigned a value to an element as a string, not a int as expected. Because of this, I decided that the ConvertTo-Json did not help. I apologize. – Aleksandr Filippov Apr 12 '21 at 19:02
  • I'd delete it, it is as you say misleading – Rno Apr 12 '21 at 20:15

1 Answers1

4

For basic authentication with Jira SERVER, the credentials need to be supplied within the header in Base64 encoding, which you need to do before supplying it via the Powershell Invoke-WebRequest method. Here's how to do it:

$username = "The username here"
$password = "The password or token here"

# Convert the username + password into a Base64 encoded hash for basic authentication
$pair = "${username}:${password}"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$headers = @{ Authorization = "Basic $base64" }

Next, in PowerShell, if you build the body of the request as a table, like you've shown, don't don't need to wrap the table elements with inverted commas, just leave them as is, but you do need to convert the table to JSON format before submitting it, like this:

$body = @{
 fields = @{
   project = @{
    key = "ProjectKey"
   }
   issuetype = @{
    id = "10705" # Or refer to the Issue type by its name eg. name = "Story"
   }
   summary = "Test"
  }
}
# Convert the body to JSON format
$body = $body | ConvertTo-Json -Depth 50

I'm assuming your $Uri string contains an actual URL to a Jira Server, not the example 'jira.domain.com'

Start with a simple request in the body, like the one I've shown, that contains only the minimum required to create the Issue, which will check your basic code is working before making the request more complex.

David Bakkers
  • 453
  • 3
  • 13
  • Many thanks for your help! ConvertTo-Json really helped me. I also had to fix the value of one of the custom fields, it turned out that int was expected in its value, not a string. But these are trifles. I also wanted to note that the quotes in the name of the elements did not affect the success of the code in any way. And, unfortunately, your code for converting username/password to Base64 returned a result that was not accepted by the Jira server. Perhaps this is because of the special characters in my password. In the end, I used the Base64 code that Postman generated for me. – Aleksandr Filippov Apr 11 '21 at 13:03
  • Glad to hear it worked out. The using of double quoting for elements in a table is only required if the word contains a space, but since the names of all the Jira fields are single words, that's why you can omit them. If the value you're setting is a number, or a single word, you can omit them there as well, such as: summary = Test, or id = 10705, but you must use them when there's a space, such as: summary = "Words with spaces" etc. You're right in that leaving them there for the Jira field names is harmless. – David Bakkers Apr 11 '21 at 20:07
  • For future generations, I have found a way to correctly convert the login password to Base64. I was writing data directly into the $pair variable, rather than using $username and $password. Thus, I did not lose special characters from the password and received the correct Base64 code. Your code is also correct, but it lost exactly the special character that I used. Thanks again for your help :-) $pair = 'YourLogin:YourPass' $Bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) $EncodedText =[Convert]::ToBase64String($Bytes) $headers = @{ Authorization = "Basic $EncodedText"} $headers – Aleksandr Filippov Apr 12 '21 at 20:54
  • Can you advise what the 'special character' is so that I can test my code to see why it doesn't work? It might be a PowerShell escape symbol or something. You said 'password', so I'm assuming you're using Jira Server, not Cloud, which must now use tokens, and those tokens are alphanumeric strings. – David Bakkers Apr 12 '21 at 21:23
  • Yes, you are right, we are using Jira Server. My password consisted of the character ` and somehow got lost in this code. $username = "Username" $password = "Abc1234``1" #there is actually one character, but I cannot write an answer for you and not break the formatting# $pair = "${username}:${password}" in this case, the variable $pair will be equal to "UserName: Abc12341" I didn't notice it right away, so I thought the whole code was wrong. But if you enter your username and password directly into $ pair, then everything is correct Like this $pair = "UserName: Abc1234`1" – Aleksandr Filippov Apr 13 '21 at 10:05