43

Am using the curl command in PowerShell to post the comment in bit-bucket pull request page through a Jenkins job. I used the below PowerShell command to execute the curl command, but am getting the error mentioned below. Could anyone please help me on this to get it worked?

$CurlArgument="-u xxx@gmail.com:yyyy -X POST https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments --data content=success"
$CURLEXE='C:\Program Files\Git\mingw64\bin\curl.exe'
& $CURLEXE $CurlArgument

Error Details:

curl.exe : curl: no URL specified!
At line:3 char:1
+ & $CURLEXE $CurlArgument
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (curl: no URL specified!:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

curl: try 'curl --help' or 'curl --manual' for more information
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

5 Answers5

24

Use splatting.

$CurlArgument = '-u', 'xxx@gmail.com:yyyy',
                '-X', 'POST',
                'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',
                '--data', 'content=success'
$CURLEXE = 'C:\Program Files\Git\mingw64\bin\curl.exe'
& $CURLEXE @CurlArgument
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
15

In Powershell 3.0 and above there is both a Invoke-WebRequest and Invoke-RestMethod. Curl is actually an alias of Invoke-WebRequest in PoSH. I think using native Powershell would be much more appropriate than curl, but it's up to you :).

Invoke-WebRequest MSDN docs are here: https://technet.microsoft.com/en-us/library/hh849901.aspx?f=255&MSPPError=-2147217396

Invoke-RestMethod MSDN docs are here: https://technet.microsoft.com/en-us/library/hh849971.aspx?f=255&MSPPError=-2147217396

Jim Moyle
  • 637
  • 4
  • 11
  • 11
    Unfortunately Invoke_WebRequest is not even close to curl on the list of what you can do with curl. Simple `--verbose` flag is not available to see the details information about TLS connection, RAW http request and response. Am I missing something? – outcoldman Dec 11 '18 at 22:26
  • very useful, thank you! sometimes we get used-to of doing things in a particular way and forget to look for alternatives. – Praveen Tiwari Apr 19 '19 at 09:48
7

Or another option you could just call curl.exe using splatting as follows.

> curl.exe '-u', 'xxx@gmail.com:yyyy',
 '-X', 'POST',
 'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',                
 '--data', 'content=success'

To know where is curl.exe using this command Get-Command curl.exe

Other option is to delete aliases curl command with Invoke-WebRequest

To see and delete aliaes in PowerShell

>Get-Aliases
>Remove-Item alias:curl

Then just runing command without '.exe'

> curl '-u', 'xxx@gmail.com:yyyy',
 '-X', 'POST',
 'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',                
 '--data', 'content=success'

I hope this would helps.

YGautomo
  • 619
  • 8
  • 12
6

PowerShell now supports default aliases, if you type help curl
You will get below output

NAME
    Invoke-WebRequest

SYNTAX
    Invoke-WebRequest [-Uri] <uri> [-UseBasicParsing] [-WebSession <WebRequestSession>] [-SessionVariable <string>] [-Credential <pscredential>] [-UseDefaultCredentials] [-CertificateThumbprint <string>] [-Certificate
    <X509Certificate>] [-UserAgent <string>] [-DisableKeepAlive] [-TimeoutSec <int>] [-Headers <IDictionary>] [-MaximumRedirection <int>] [-Method {Default | Get | Head | Post | Put | Delete | Trace | Options | Merge | Patch}] [-Proxy
    <uri>] [-ProxyCredential <pscredential>] [-ProxyUseDefaultCredentials] [-Body <Object>] [-ContentType <string>] [-TransferEncoding {chunked | compress | deflate | gzip | identity}] [-InFile <string>] [-OutFile <string>] [-PassThru]
     [<CommonParameters>]


ALIASES
    iwr
    wget
    curl


REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help Invoke-WebRequest -Online" or
           go to https://go.microsoft.com/fwlink/?LinkID=217035.

So to download a file you can type

curl -Uri "https://www.example.com/myfile.txt" -OutFile myfile.txt
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
  • Thank you! There were other answers above that pointed to Invoke-WebRequest, but this one showed how it was an alias and gave a concise example. Very helpful answer for the simplest use case where I don't need to be syntatically equivalent with curl. – jg3 Nov 28 '22 at 18:40
  • Please note that `curl` was removed as an alias in PowerShell Core. – lit Mar 29 '23 at 12:50
5

Instead of curl you can use this command:

(New-Object System.Net.WebClient).DownloadString("http://google.com")
blackgreen
  • 34,072
  • 23
  • 111
  • 129
Harsha
  • 61
  • 1
  • 2