简体   繁体   English

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

[英]convert curl command to invoke-WebRequest or Invoke-RestMethod

I have a requirement to convert a working curl command to Invoke-WebRequest command The command is used to create a project in SonarQube 我需要将一个有效的curl命令转换为Invoke-WebRequest命令。该命令用于在SonarQube中创建项目

Curl: 卷曲:

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

the command I tried: 我尝试的命令:

$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

Error: 错误:

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

Does anyone have an idea of converting curl to invoke-webrequest 有没有人想将curl转换为invoke-webrequest

This has been asked before. 这已经被问过了。 When you are posting, you need to have a body to send too, example: 发布时,也需要有一个要发送的正文,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM