user6387287
user6387287

Reputation:

Rewriting Unix Curl Commands for PowerShell Invoke-WebRequest

I have a unix cmd that I need to rewrite to achieve the same result in Windows PowerShell. I am specifically stuck on the login...

Original Unix Command:

curl -u user:xxxx -X POST -s --data '{"one" : "one/text","two" : "two/text"}', http://myurl.net/run

My Attempted Conversion:

$params = '{"one" : "one/text","two" : "two/text"}'
Invoke-WebRequest -Uri http://myurl.net/run -Credential user:xxxx -Method POST -Body $params

Right now, I get the error:

Invoke-WebRequest : {"mimetype":[{"stderr":"text/plain"}],"streamdata":[{"stderr":"User cannot be authenticated"}],"status":"error"}

Thank you for any pointers or help!!

Upvotes: 1

Views: 2520

Answers (1)

Henrik Stanley Mortensen
Henrik Stanley Mortensen

Reputation: 1069

To me it looks you the error is with your -Credential param.
This expects a credential object, not a string.

Try:

$Credential = Get-Credential
$params = '{"one" : "one/text","two" : "two/text"}'
Invoke-WebRequest -Uri http://myurl.net/run -Credential $Credential -Method POST -Body $params

The Get-Credential command will promt you to enter Username and Password.

Also, if your endpoint is NOT https then you need to tell Powershell with the use of the following switch -AllowUnencryptedAuthentication paramter or it will always fail according to the documentation:

Allows sending of credentials and secrets over unencrypted connections. By default, supplying -Credential or any -Authentication option with a -Uri that does not begin with https:// will result in an error and the request will abort to prevent unintentionally communicating secrets in plain text over unencrypted connections. To override this behavior at your own risk, supply the -AllowUnencryptedAuthentication parameter.

Warning: Using this parameter is not secure and is not recommended. It is provided only for compatibility with legacy systems that cannot provide encrypted connections. Use at your own ris

The link to the cmdlet documentation on MS Docs

Upvotes: 2

Related Questions