How do i convert this curl command to powershell? This works perfectly using curl however in powershell i am geting 401 unauthorized.
I have tried everything i can think of. This code can only be executed inside a certain environment. i am trying to pass a cookie i know is valid into the header of the second. The first request works well, the second request which is meant the return json does not. Instead returns 401
The command requires cookies to authenitcate
curl.exe -vu SuperGabriel:SuperGabriel#2019 -X POST -H "X-Application: 3rdParty" https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login --insecure
curl.exe -v -H "Cookie: thirdParty_SESSIONID=6483556424564819468" -H "X-
Application: 3rdParty" https://webadmin.td90.centile-dev.com/restletrouter/v1/3rdParty/AdmtiveDomain --insecure
curl.exe -v -H "Cookie: thirdParty_SESSIONID=1312545750448673312" -H "X-Application: 3rdParty" https://webadmin.td90.centile-dev.com/restletrouter/v1/3rdParty/CallRecord/0.106.?day=20190301 -k --insecure -o
"C:\Users\stephenm\Documents\test.csv"
Here is my powershell code so far.
add-type #"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"#
[System.Net.ServicePointManager]::CertificatePolicy = New-Object
TrustAllCertsPolicy
$user = "SuperGabriel"
$pass = "SuperGabriel#2019"
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$headers = #{
"Authorization" = $basicAuthValue
"X-Application" = "3rdParty"
}
$login = Invoke-WebRequest -Uri https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login -Headers $headers -Method Post
$headers = #{
"Authorization" = $basicAuthValue
"X-Application" = "3rdParty"
"Cookie" ="thirdParty_SESSIONID=4436753218874838616"
"Content-Type" = "application/json"
}
try
{
$admtiveDomains = Invoke-WebRequest -Uri https://webadmin.td90.centile-
dev.com/restletrouter/v1/3rdParty/AdmtiveDomain -Headers $headers -Method
Get
}catch{
echo $ErrorMessage = $_.Exception.Message
}
many thanks
Is it possible you need to get a token first that you then need to convert to a base64 string?
$uri = 'https://webadmin.td90.centile-dev.com/restletrouter/v1/service/Login'
$body = #{ username = 'SuperGabriel'; password = 'SuperGabriel#2019' }
$bodyjson = $body | convertTo-Json
$ctype = "application/json"
$token = invoke-restmethod -uri $uri -method POST -body $bodyjson -contenttype $ctype
I don't know what object $token would return, but you would essentially then convert the returned string and continue on from there.
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($token))
Related
I am struggling with this code as I have never encountered
Here is the Example code that click send provides.
curl --include \
--header "Authorization: Basic YXBpLXVzZXJuYW1lOmFwaS1wYXNzd29yZA==" \
--request POST \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-binary "username=myusername&key=1234-I3U2RN34IU-43UNG&to=61411111111,64122222222,61433333333&senderid=example&message=testing" \
'https://api-mapper.clicksend.com/http/v2/send.php'
URL: https://developers.clicksend.com/docs/http/v2/#send-an-sms
Here is what I have figured out so far:
$BaseURL = "https://api-mapper.clicksend.com/http/v2/send.php"
$Header = #{
"Authorization" = "Basic"+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$APIKey"))
}
$body = #{
"useranme"="$Username"
"key"="$APIKey"
"to"="$To"
"senderid"="$from"
"message"=$Message
}
$Return = Invoke-RestMethod -Method Post -Headers $Header -ContentType "application/x-www-form-urlencoded" -Uri $BaseURL -Body $body
$return.InnerXml
I'm stuck on the --data-binary part of the code.
I believe using the --data-binary in curl is like creating a string in the body of the request:
$BaseURL = "https://api-mapper.clicksend.com/http/v2/send.php"
$Username = "Username"
$APIKey = "SomeAPIKey"
$To = "Recipient"
$From = "Sender"
$Message = "MessageFoRecipient"
$Header = #{
"Authorization" = "Basic"+ " " + ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$APIKey")))
}
$body = "username=$Username" + "&" + "key=$APIKey" + "&" + "to=$To" + "&" + "senderid=$From" + "&" + "message=$Message"
$return = Invoke-RestMethod -Method Post -Headers $Header -ContentType "application/x-www-form-urlencoded" -Uri $BaseURL -Body $body
$return.InnerXml
Invoke-RestMethod assumes that you want your POST body to be application/x-www-form-urlencoded (unless otherwise specified, e.g. XML, JSON). You must be having an issue with something such as the fact that the Authorization header doesn't have a space between Basic and the Base64 string, or the fact that username has a typo.
$BaseURL = 'http://localhost:8000'
$Username = 'api-username'
$ApiKey = 'api-password'
$To = '61411111111,64122222222,61433333333'
$From = 'example'
$Message = 'testing'
$SenderId = 'example'
$Body = #{
username = $Username
key = $ApiKey
to = $To
senderid = $SenderId
message = $Message
}
$Header = #{
Authorization = 'Basic ' + ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$APIKey")))
}
$Return = Invoke-RestMethod -Method Post -Headers $Header -Uri $BaseURL -Body $Body
$Return.InnerXml
Yields the following HTTP request:
POST / HTTP/1.1
Host: localhost:8000
Authorization: Basic YXBpLXBhc3N3b3Jk
User-Agent: Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.19042; en-US) PowerShell/7.2.1
Content-Length: 112
Content-Type: application/x-www-form-urlencoded
key=api-password&message=testing&username=api-username&to=61411111111%2C64122222222%2C61433333333&sender=example
Am Currently trying to query the authToken
$uri = 'http://xxx.centreon/api/index.php?action=authenticate'
$headers = #{
'username' = 'exemple'
'password' = 'exemple'
}
Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -ContentType application/x-www-form-urlencoded
But am having bad parameters
Via linux : curl -s -d "username=exemple&password=exemple" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://xxx.centreon/api/index.php?action=authenticate
{"authToken":"NWU5OWM0YjRhMjIzMDYuMzg2NzYyMDY="}-
So this is a vague question but i do see how things might be wrong.
First the Username and Password are usually in the Body not the Header.
In your curl command I see its being passed as -d which would be the Body
$uri = 'http://xxx.centreon/api/index.php?action=authenticate'
$Body = #{
'username' = 'exemple'
'password' = 'exemple'
}
Invoke-RestMethod -Uri $uri -Method Post -body $Body -ContentType "application/x-www-form-urlencoded"
I have follwoing curl command which I would like to convert to PowerShell (PowerShell v3.0):
curl -s -k -H 'Authorization: api_key 1234567890' -F upload_file=#"C:\path\to\file" -X POST "https://my.url.com/submit"
What I have so far:
$Body= #{ "upload_file" = "C:\path\to\file" };
$Headers = #{ "Authorization" = "api_key 1234567890" };
$Uri = "https://my.url.com/submit"
Invoke-WebRequest -Method Post -Uri $Uri -H $Headers -Body $Body
I think the form parameter is the issue.
Thanks in advance.
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
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%"
I'm trying to use the Invoke-RestMethod cmdlet in PowerShell 3 and 4, to upload a large binary file using a REST API's multipart/form-data upload. Here is a working cURL example on how to perform what I want to do in PowerShell:
curl -i -k -H "accept: application/json" -H "content-type: multipart/form-data" -H "accept-language: en-us" -H "auth: tokenid" -F file="#Z:\large_binary_file.bin" -X POST "https://server/rest/uri2"
I would love to see a working example on how to use Invoke-RestMethod to POST a multipart/form-data. I found a blog post from the PowerShell team showing how to use Invoke-RestMethod to upload to OneDrive (aka SkyDrive), but doesn't work well. I'd also like to avoid using System.Net.WebClient if at all possible. I also found another thread here on Stackoverflow, but it really didn't help much.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$server = "https://server"
uri = "/rest/uri1"
$headers = #{"accept" = "application/json"; "content-type" = "application/json";"accept-language" = "en-us"}
$body = #{"userName" = "administrator"; "password" = "password"}
$method = "POST"
#Get Session ID
$resp = Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body (convertto-json $Body -depth 99)
$sessionID = $resp.sessionID
#Upload file
$uri = "/rest/uri2"
$headers = #{"accept" = "application/json";"content-type" = "multipart/form-data"; "accept- language" = "en-us"; "auth" = $sessionID}
$medthod = "POST"
$largeFile = "Z:\large_binary_file.bin"
I have tried both ways of using Invoke-RestMethod:
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -InFile $largeFile
or
$body = "file=$(get-content $updateFile -Enc Byte -raw)"
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body $body
I notice couple of mistakes in your invoke statement. First, you need to use POST keyword instead of string value. Second, make sure that Content-Type is set to multipart/form-data. So this is my revised statement -
$uri = "http://blahblah.com"
$imagePath = "c:/justarandompic.jpg"
$upload= Invoke-RestMethod -Uri $uri -Method Post -InFile $imagePath -ContentType 'multipart/form-data'
You can check the response from the server by checking $upload.