将curl命令转换为invoke-WebRequest或Invoke-RestMethod

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

我需要将一个工作的curl命令转换为Invoke-WebRequest命令该命令用于在SonarQube中创建一个项目

卷曲:

curl -u as123123112312: -X POST "http://sonarqube.it.company.net:9000/api/projects/create?key=%project_key%&name=%project_name%" > NUL

我试过的命令:

$e = @{
    Uri     = "http://sonarqube.it.company.net:9000/api/projects/create?key=%project_key%&name=%project_name%"
    Headers = @{"Authorization" = "Token as123123112312"}
}
Invoke-WebRequest @e -Method POST

错误:

Invoke-WebRequest : The remote server returned an error: (401) Unauthorized

有没有人知道将curl转换为invoke-webrequest

powershell curl sonarqube invoke-command
1个回答
-1
投票

以前曾经问过这个问题。发布时,您还需要有一个正文发送,例如:

$username = "as123123112312";
$password = "blah";
$Bytes = [System.Text.Encoding]::UTF8.GetBytes("$username`:$password");
$encodedCredentials = [System.Convert]::ToBase64String($bytes);
$body = "your content (i.e. json here)";
Invoke-WebRequest -Method Post -Body $body -Headers @{ "Authorization" = "Basic " + $encodedCredentials} -Uri "http://sonarqube.it.company.net:9000/api/projects/create?key=%project_key%&name=%project_name%"
© www.soinside.com 2019 - 2024. All rights reserved.