json - 发布 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'
解决方案
使用 cmdlet 检查 PS 内的文档get-help Invoke-RestMethod -Examples
。有一个关于如何处理 POST 请求的示例。不要在标头上设置您的凭据,而是创建一个 Credentials 对象并-Credentials
使用Invoke-RestMethod
.
凭证的处理方式通常不同于 GET 请求和 REST 上的 POST 请求。
另一方面,我建议您改用此JiraPS 模块,这样您就无需重新发明轮子并专注于完成工作!
推荐阅读
- linux - 查找带有可选子表达式的命令正则表达式
- python - 我想在视频中定义一个感兴趣区域并只处理该区域
- javascript - 如何防止重复进入购物车?如果尝试然后想要 qty++。jQuery
- wso2 - WSO2 产品中的 MQTT 异常
- binary - 二进制补码的左移操作
- jupyter - 尝试在 jupyter notebook 中导入一些空间分析库时出错:ImportError: DLL load failed: El sistema operativo no puede ejecutar %1
- php - 无法使用 https SOAP 连接到主机,但使用 http 正常
- jenkins - 每个詹金斯工作都需要专用的 Xvfb 显示器
- java - 在 Appium 中出现错误“iproxy exited with code '208'”并且应用程序未在 iOS 移动设备上启动
- windows - 为什么从make文件中查找命令失败