PowerShell equivalent for cURL command uploading file - windows

I am able to send HTTP POST a text file to my server using the following cURL command:
curl -i -F file=#file.txt http://www.website.com/put_file
The server is expecting a file from $_FILES['file'].
I have the following so far but it is not working:
$url = http://www.website.com/put_file
$file = "c:\file.txt"
$wc = new-object System.Net.WebClient
$wc.UploadFile($url, $file.FullName)
it returns 0, and it did not upload to the server
How can I send the file as $_FILES['file']? Also, how can I see the response from the server?

I think it should be something like this:
$body = "file=$(get-content file.txt -raw)"
Invoke-RestMethod -uri http://www.websetite.com/put_file -method POST -body $body
Note: this does require PowerShell V3. Also, you might need to specify the -ContentType parameter as "application/x-www-form-urlencoded". I recommend getting Fiddler2 and use it to inspect the request in both the curl and Inovke-RestMethod cases to help get the irm parameters correct. This is assuming the file contents are fairly small and non-binary. If not, you probably need to go to content type multipart/form-data.

In Powershell you can use curl.exe instead of curl (yes, are different commands: http://get-cmd.com/?p=5181)
curl.exe -i -F file=#file.txt http://www.website.com/put_file
Or you can use Invoke-RestMethod from powershell 3
More details about Invoke-RestMethod from powershell 3 at: https://superuser.com/questions/344927/powershell-equivalent-of-curl

To solve this, I download curl.exe for windows, and still used curl.

After a couple hours of using fiddler to try to match curl, I gave up and just used this guy's function (See link below). Worked like a charm.
http://blog.majcica.com/2016/01/13/powershell-tips-and-tricks-multipartform-data-requests/

Related

How do I query Azure DevOps (TFS) using curl.exe or PowerShell directly without additional support libraries?

Azure DevOps has a REST API: https://learn.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-7.1
How do I access it using curl.exe or PowerShell without additional support libraries?
This instructions are for Windows, although they can be used in other platforms with slight modification.
Below, replace with your own values for:
{organization}
{project}
{team}
{pat}
You can create a PAT in your organization's portal. Using your browser, go to: (replace {organization}):
https://dev.azure.com/{organization}/_usersSettings/tokens
Using Command Prompt
If using Command Prompt (cmd.exe), curl.exe can be used. It is part of Windows (10+). Also works in PowerShell, but you have to spell out curl.exe, not just curl. The commands below output the results to the console. You can pipe it to a file, or use the -o option in curl to specify a file to output to.
Using PoowerShell
If using PowerShell, you use the Invoke-RestMethod command. In the below commands, the result of the command is placed in a variable, $result, which you can use after the invocation.
The commands below also assume variables $pat and $header are initialized as follows:
$pat = "{pat}"
$header = #{authorization = "Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)")))"}
Example getting a work item by ID (Work Items - Get Work Item):
Using curl:
curl.exe -u :{pat} -H "Content-Type: application/json" https://dev.azure.com/{organization}/_apis/wit/workItems/{id}
Using PowerShell:
$url = "https://dev.azure.com/{organization}/_apis/wit/workItems/{id}"
$entries = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
Quick display of the above output:
$entries.Fields | Format-Table system.workitemtype, system.title

Bash on Azure Pipeline running windows agent

I'm trying to get a build number from a secondary Azure pipeline. I'm using bash on a windows agent, already enabled developer mode, and installed the Linux feature. When I test this locally it works, I'm able to connect and get the build number but from the pipeline, I get the error...
Enhanced Security Configuration is currently enabled on your environment. This enhanced level of security prevents our web integration experiences from displaying or performing correctly. To continue with your operation please disable this configuration or contact your administrator
Could it be related to the Auth system token? I have also enabled "Allow scripts to access the OAuth token"
Bash code that I'm trying to use on the pipeline:
#!/bin/bash
echo $TAG
echo $VER
echo $SYSTEM_ACCESSTOKEN
echo "Executing call to get latest build from a pipeline definition Id"
echo "$URL-7.1"
export RESPONSE=`curl -u username":$SYSTEM_ACCESSTOKEN" --location --request GET "$URLapi-version=7.1-preview.1"`
echo $RESPONSE
export BUILD_NUMBER=`echo $RESPONSE | jq -r '.["buildNumber"]'`
echo $BUILD_NUMBER
Powershell script working locally, but if I use this form, the access token is not recognized.
$connectionToken= $(System.AccessToken)
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$BuildPipelineUrl = "$url`$top=1&api-version=6.0"
Write-Host "URL: $BuildPipelineUrl"
$BuildPipelineInfo = (Invoke-RestMethod -Uri $BuildPipelineUrl -Method Get -UseDefaultCredential -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)})
$ver = $BuildPipelineInfo.value.buildnumber
Write-Host "$ver"
Is there another way to use the Sys auth token, Do I have to convert it?
Thanks, everyone!.
You could try to use the following powershell script to check if it works.
$Token = $env:SYSTEM_ACCESSTOKEN
$BuildPipelineUrl = "$url`$top=1&api-version=6.0"
Write-Host "URL: $BuildPipelineUrl"
$BuildPipelineInfo = (Invoke-RestMethod -Uri $BuildPipelineUrl -Method Get -Headers #{ Authorization = "Bearer $Token" })

Curl Upload-File inside Shell Runner

I am trying to upload a single file via curl from within a gitlab-shell-runner.
Basically I need to use this command:
curl --user <user:password> --insecure --upload-file <file> https://example.com
Following error is shown:
Invoke-WebRequest : A positional parameter cannot be found that accepts argument "<user:password>"
Which adjustments do I need to make, to run this inside gitlab-ci?
I have already tried to use a variable for the address and different variation of quotations.
Thank you in Advance!
As mentioned in "Running cURL on 64 bit Windows" you are running a CmdLet called Invoke-WebRequest, which has a curl alias in Powershell.
Start by removing it:
Remove-item alias:curl
Then check if your script does call the actual curl command (or you might need to call it explicitly).
Or: use Invoke-WebRequest (aliased 'curl') with a Basic $encodedCredentials header.

trying to convert simple curl command to invoke-webrequest but not working

I have a very simple curl command, this works for me (not using basic auth- the service just expects the token to be in URL):
curl -v -X POST https://Dkdjhf928hfDJFSDFJSK#sonarcloud.io/api/projects/create -d "name=sdfsdfsdf&project=sdfsdfdsf&organization=zzzzz"
This does not work:
Invoke-WebRequest -Method POST `
-Body 'name=sdfsdfsdf&project=sdfsdfdsf&organization=zzzzz' `
-Uri 'https://Dkdjhf928hfDJFSDFJSK#sonarcloud.io/api/projects/create'
powershell must be mangling something because I get this error:
Invoke-WebRequest : {"errors":[{"msg":"Insufficient privileges"}]}
Is there some default way curl sends body or data that powershell does not?

How to upload .jtl file to blazemeter sense

I created a Jenkins job where it run some JMeter scripts and return .jtl files. Now, I want to upload this files to Blazemeter Sense to see the performance test, downloads pdf reports, etc.
I've searched a lot of information, where I find that to upload some file, I can use this command thats running from Windows CMD:
curl -v https://sense.blazemeter.com/api/files -H "Authorization: Token 'cat ~/.loadosophia.token'" -F "projectKey=Project_name" -F "jtl_file=#jtl.gz"
REFERENCE: https://sense.blazemeter.com/wiki/help:uploads/
The only values that I change are
cat ~/.loadosophia.token, where I replaced for my Upload Token (finded in Blazemeter Sense -> Options -> Settings -> Your Upload Token)
projectKey where I replaced by my project name (test_taurus)
jtl_file where I replaced by the file directory generated by Jmeter test (.jtl)
The final command is:
curl -v https://sense.blazemeter.com/api/files -H "Authorization: Token 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXYLaa'" -F "projectKey=test_taurus" -F "jtl_file=/path/of/file/file.jtl"
I missed some? What is my wrong? Existe another posibility to make it?
Thanks everyone
U P D A T E:
I did what Dmitri T said. Thats works. But when I run the command, the output is the following:
What could be the issue?
You need to remove quotation marks around the token
You need to add an "at" symbol before the .jtl file path
Fixed command would be something like:
curl -v https://sense.blazemeter.com/api/files -H "Authorization: Token XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXYLaa" -F "projectKey=test_taurus" -F "jtl_file=#/path/of/file/file.jtl"
More information: Upload files with CURL
You might find BM.Sense Uploader plugin more convenient to use, the plugin can be installed using JMeter Plugins Manager:

Resources