Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 转换curl命令以调用WebRequest或调用RestMethod_Powershell_Curl_Sonarqube_Invoke Command - Fatal编程技术网

Powershell 转换curl命令以调用WebRequest或调用RestMethod

Powershell 转换curl命令以调用WebRequest或调用RestMethod,powershell,curl,sonarqube,invoke-command,Powershell,Curl,Sonarqube,Invoke Command,我需要将工作curl命令转换为调用WebRequest命令 该命令用于在SonarQube中创建项目 卷曲: 我尝试过的命令: $e = @{ Uri = "http://sonarqube.it.company.net:9000/api/projects/create?key=%project_key%&name=%project_name%" Headers = @{"Authorization" = "Token as123123112312"} } Inv

我需要将工作curl命令转换为调用WebRequest命令 该命令用于在SonarQube中创建项目

卷曲:

我尝试过的命令:

$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转换为调用webrequest呢?这是以前提出的问题。发帖时,您还需要发送正文,例如:

$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%"

尝试使用
正文
发送授权标头,而不是
标头
参数。您的意思是这样的$e=@{Uri=”“Body=@{“Authorization”=“Token as123112312”};我尝试了这个,但仍然显示相同的错误不,它不起作用,得到了名称错误。“AS123112312”不是用户名。这是一个用户令牌,用于调用sonarqube Webservices。似乎除了Powershell之外,您也不知道如何使用curl。在您的问题中,您指定了curl命令“curl-u as123112312:”,这是一个用户名:密码身份验证转换为我在Powershell中粘贴的内容。是的,可能是这样,我对curl没有太多的了解。。无论如何,“as12…”是我从sonarqube生成的令牌,当在linux shell脚本中与curl命令一起使用时,它起作用。我的要求是在powershell中创建一个类似的脚本。上一天我一直在寻找解决方案,但没有找到任何解决方案,这就是为什么我在这里问这个问题。
$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%"