首页 > 解决方案 > 发布 Jira 适用于 Python,但不适用于 PowerShell

问题描述

我正在尝试使用脚本向 Jira 发送 POST 请求,它在 Python 中运行良好,但在 PowerShell 中运行良好。

我尝试 GET 请求以使用 PowerShell 从 Jira 获取一些数据并且它有效,所以我猜登录详细信息是正确的。所以我唯一关心的是有效载荷。也许我的 JSON 格式不正确。

Python(作品):

headers = {
  "Accept": "application/json;charset=utf-8",
  "Content-Type": "application/json"
}

payload = {
  "fields": {
    "project": {
      "key": "SDF"
    },
    "summary": "test",
    "description": "Creating of an issue using project keys and issue type names using the REST API",
    "issuetype": {
      "name": "Request"
    }
  }
}

password = open('passwords_jira2snow.txt').readlines()
jira_login = HTTPBasicAuth(password[0].rstrip('\n'), password[1].rstrip('\n')) 

r = requests.post(url="domain/rest/api/2/issue", data = json.dumps(payload), auth=jira_login, verify=False, headers=headers)
#jira_response = json.loads(r.text)
print(r.text)

PowerShell(不起作用):

$payload = ('{
  "fields": {
    "project": {
      "key": "SDF"
    },
    "summary": "test",
    "description": "Creating of an issue using project keys and issue type names using the REST API",
    "issuetype": {
      "name": "Request"
    }
  }
}')
$hdrs1 = @{}
$hdrs1.Add("login",$login)
$hdrs1.Add("password",$password)

$CreateJira = Invoke-RestMethod -Method POST -Headers $hdrs1 -Uri "domain/rest/api/2/issue" -Body $payload -ContentType "application/json"
Write-Host $CreateJira

我越来越

错误:对 sessionauth 端点的 REST 调用失败,出现错误“ProtocolError -
System.Net.WebException:远程服务器返回错误:(400)错误请求。
     在 Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest 请求)
     在 Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()'
当前端点:'https://domain/sessionauth/v1/authenticate'

标签: jsonpowershelljirajira-rest-api

解决方案


使用 cmdlet 检查 PS 内的文档get-help Invoke-RestMethod -Examples。有一个关于如何处理 POST 请求的示例。不要在标头上设置您的凭据,而是创建一个 Credentials 对象并-Credentials使用Invoke-RestMethod.

凭证的处理方式通常不同于 GET 请求和 REST 上的 POST 请求。

另一方面,我建议您改用此JiraPS 模块,这样您就无需重新发明轮子并专注于完成工作!


推荐阅读