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

时间:2018-09-17 12:30:05

标签: powershell curl sonarqube invoke-command

我需要将有效的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的想法

1 个答案:

答案 0 :(得分:-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%"