0

When I try to create an issue in redmine via the Rest Api using Powershell, I get an error: The remote server returned an error: (422) Unprocessable Entity. In postman, this request is processed correctly.

Here is an example of script:

$Headers = @{ 'X-Redmine-API-key' = '********' }
$Uri = "https://task.test.com/issues.json"
Invoke-RestMethod $Uri -Headers $Headers -Method POST -ContentType 'application/json' -Body '{
    "issue": {
        "project_id": 316,
        "subject": "testProject",
        "priority_id": 4,
        "description": "testTask",
        "assigned_to_id": 1891,
        "custom_fields": [{
                "value": "******",
                "name": "physicalName",
                "id": 5
            },
            {
                "value": "****",
                "name": "***",
                "id": 6
            },
            {
                "value": "****",
                "name": "*****",
                "id": 10
            },
            {
                "value": "****",
                "name": "*****",
                "id": 11
            },
            {
                "value": "0",
                "name": "*******",
                "id": 16
            }
        ]
    }
}'
Pomidor_83
  • 11
  • 1
  • 3
  • Your header is wrong should be : Headers = @{ 'X-Redmine-API-key = ********' } You have extra single quotes. – jdweng Jan 10 '23 at 10:07
  • @jdweng - the op's ```$Headers = ...``` *syntax* is fine - hashtables are ```@{ "key" = "value" }```, not ```@{ "key = value" }```. If you try your version in a powershell console you get ```ParserError: Missing '=' operator after key in hash literal.```... – mclayton Jan 10 '23 at 10:33
  • @mclayton : It is an array of HTTP headers. Each header needs to be an entity. What the OP had is wrong, and you are wrong. – jdweng Jan 10 '23 at 10:36
  • If I run ```$Headers = @{ 'X-Redmine-API-key = ********' }``` in a powershell console I get the error ^^^. The docs for ```Invoke-RestMethod``` (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.3) also say ```[-Headers ]``` so \o/... – mclayton Jan 10 '23 at 10:40
  • Headers are OK. Problem has been with encoding (in ps default is UTF16). $body = [System.Text.Encoding]::UTF8.GetBytes($body); - helped for me. – Pomidor_83 Jan 20 '23 at 11:27

0 Answers0