发布Jira在Python上运行,但在PowerShell上不运行

问题描述 投票:0回答:2

[我正在尝试使用脚本向Jira发送POST请求,并且它在Python上工作正常,但在PowerShell中却没有。

我尝试使用PowerShell进行GET请求以从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”
json powershell jira jira-rest-api
2个回答
1
投票

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

与REST上的POST请求相比,凭据与GET请求的处理通常不同。

另一方面,我建议您改用此JiraPS Module,因此您无需重新发明轮子,而不必专注于完成工作!


0
投票

[不知道为什么,但是以其他方式定义凭据很有帮助。很奇怪,因为默认情况下GET请求有效。

 $login = "test"
 $password = "test"  
 $pair = "$($login):$($password)"
 $encodedCreds = 
 [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))  
 $hdrs1.Add("Authorization", "Basic $encodedCreds")  
© www.soinside.com 2019 - 2024. All rights reserved.