Curl To Powershell - Click Send

Viewed 83

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.

2 Answers

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
Related