65

I am using the curl command in PowerShell to post the comment on the Bitbucket pull request page through a Jenkins job. I used the below PowerShell command to execute the curl command, but I am getting the error mentioned below. How can I get it to work?

$CurlArgument="-u [email protected]: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

2
  • 2
    Sorry, don't have any experience with curl but why don't you use the built-in PowerShell Invoke-WebRequest cdmlet? Commented Oct 18, 2016 at 10:19
  • @MartinBrandl, Am not aware of that, i will check about it and let you know. Commented Oct 18, 2016 at 10:21

6 Answers 6

31

Use splatting.

$CurlArgument = '-u', '[email protected]: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
Sign up to request clarification or add additional context in comments.

4 Comments

Am getting the below error. + CategoryInfo : NotSpecified: ( % Total % ... Time Current:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
That's curl status output. Add the parameter -s to suppress it.
$CurlArgument = '-s', '-u', '[email protected]:yyyy', '-X', 'POST', 'xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/…', '--data', 'content=success'
much easier than trying to use Invoke-RestMethod in PowerShell when it comes to using content-type: multipart/form-data :) Thanks
20

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

curl.exe '-u', '[email protected]:yyyy',
 '-X', 'POST',
 'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',
 '--data', 'content=success'

To know where curl.exe is, use the command Get-Command curl.exe.

Another option is to delete the aliases curl command with Invoke-WebRequest.

To see and delete aliases in PowerShell:

Get-Aliases
Remove-Item alias:curl

Then just running the command without '.exe':

curl '-u', '[email protected]:yyyy',
 '-X', 'POST',
 'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',
 '--data', 'content=success'

2 Comments

Get-Aliases didn't work. However, Remove-Item finally made my curl work! as I had a scoop installation of it.
As of now, using curl.exe should work fine with no adjustments needed to the command. For example curl.exe -X -i GET '/' localhost:3000
18

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

2 Comments

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?
very useful, thank you! sometimes we get used-to of doing things in a particular way and forget to look for alternatives.
11

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

4 Comments

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.
Please note that curl was removed as an alias in PowerShell Core.
How can I use above command to download a file using username/password credentials? the -Credentials 'username:password' takes username:password as just the username, and prompts for a password.
you can use `-Headers "Basic <base64>"
6

Instead of curl, you can use this command:

(New-Object System.Net.WebClient).DownloadString("http://google.com")

Comments

-2

The simple solution is to use the Git Bash command line tool instead of Windows PowerShell. :)

2 Comments

This is very unhelpful from the context of the question. If an honest suggestion, it might very well be a comment on the original post.
The question is: "How can I use the curl command in PowerShell?"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.