1

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:

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

Krishh
  • 103
  • 1
  • 8
  • try to send the Authorization header with the `Body` instead of the `Headers` parameter – Avshalom Sep 17 '18 at 12:53
  • did you mean like this $e = @{ Uri = "http://sonarqube.it.company.net:9000/api/projects/create?key=%project_key%&name=%project_name%" Body = @{"Authorization" = "Token as123123112312"} } ; I tried this and still showing same error – Krishh Sep 17 '18 at 13:14

1 Answers1

-1

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%"
Iggy Zofrin
  • 515
  • 3
  • 10
  • No, it's not working, getting the name error. "as123123112312" is not the username. It's a user token to invoke sonarqube webservices – Krishh Sep 18 '18 at 07:36
  • It seems apart from Powershell you don't understand how to use curl either. In your question you specify your curl command "curl -u as123123112312: ", that is a username:password authentication translated to exactly what I pasted in Powershell. – Iggy Zofrin Sep 18 '18 at 08:30
  • yeah could be and I don't have much knowledge in curl.. anyway the "as12..." is the token I generated from sonarqube and when used with the curl command in a linux shell script it worked. My requirement is create a similar script in powershell. I've been looking for a solution last whole day and couldn't find any, that is why I asked the question here. – Krishh Sep 18 '18 at 08:57