I'm trying to convert a HTTP request (which I've never dealt with before) into a cURL and Powershell command, but I'm not getting anywhere....can someone help me fill in the blanks?
POST /api/users.profile.set
Host: slack.com
Content-type: application/json; charset=utf-8
Authorization: bearer_token
{
"profile": {
"status_text": "Eating some french fries from the frituur",
"status_emoji": ":fries:"
}
}
curl 'https://slack.com/api/users.profile.set' `
--header 'Authorization: bearer_token' `
--header 'Content-Type: application/json' `
--data-raw "{
\"profile\": {
\"status_text\": \"On Lunch\",
\"status_emoji\": \":hamburger:\"
}
}"
and
Invoke-WebRequest -Headers #{"Authorization" = "bearer_token"} `
-Method POST `
-Uri https://slack.com/api/users.profile.set `
-ContentType application/json
Fixed it myself.
curl -X POST 'https://slack.com/api/users.profile.set' `
-H 'Authorization: Bearer xoxp-xxxxxxxxxxxxxxxxx' `
-H 'Content-Type: application/json; charset=utf-8' `
-d '{
\"profile\": {
\"status_text\": \"Sick\",
\"status_emoji\": \":sickpepe:\",
\"status_expiration\": \"480\"
}
}'
$body = #{
profile = #{
status_text = "Eating some french fries from the frituur"
status_emoji = ":fries:"
status_expiration = 60
}
}
Invoke-WebRequest -Headers #{"Authorization" = "Bearer xoxp-xxxxxxxxxxxxxxxxxxxx"} `
-Method POST `
-Uri https://slack.com/api/users.profile.set `
-ContentType 'application/json; charset=utf-8' `
-Body ($body|ConvertTo-Json)
```
Related
I have been challenged to get a response from an api with powershell.
I have little to no experience with powershell, but have at least been able to get a response from the API provider, but not a response i can use, so i hope someone can help me.
The photo is of the documentaion i have
Documentation
What i can't figure out is how i send the message that i want a response for.
The command is the specific api that i need to use, and it has some inputs as seen in the photo.
Input to API:
StreetName
BuildingIdentifier
Floor
Suite
DistrictCode
This is what i got so far.
$uri = 'SomeUrl.com'
$command = '/Ois/RealUnit/Address'
$token = 'SomeSecureToken'
$contentType = 'application/json'
$secureToken = ConvertTo-SecureString $token -AsPlainText -force
$webResult = Invoke-RestMethod -Method get -Uri $uri -ContentType $contentType -Token $secureToken
write-output $webResult
What i know but i don't know how to use or i it has to be used is as follows.
Request URL: https://SomeUrl.com/Ois/RealUnit/Address
Response body: is JSON and i only need the response for "unitUse"
Response Code: I also know that a good response gives the code 200
Curl: I also know that something called CURL looks a bit like this curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'StreetName=STREET&BuildingIdentifier=123&Floor=12&Suite=th&DistrictCode=1234' 'https://SomeUrl.com/Ois/RealUnit/Address'
Reponse Header: Looks like this
{ "access-control-allow-headers": "Content-Type", "access-control-allow-methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS", "access-control-allow-origin": "*", "cache-control": "private", "content-length": "7604", "content-type": "application/json; charset=utf-8", "date": "Tue, 08 Mar 2022 13:21:27 GMT", "server": "Microsoft-IIS/8.5", "strict-transport-security": "max-age=31536000", "vary": "Accept", "x-aspnet-version": "4.0.30319", "x-powered-by": "ServiceStack/5.10 NET45 Win32NT/.NET, ASP.NET" }
$uri = 'https://SomeUrl.com/Ois/RealUnit/Address'
$token = 'SomeSecureToken'
$contentType = 'application/json'
$secureToken = ConvertTo-SecureString $token -AsPlainText -force
$Body = #{
StreetName = "STREET"
BuildingIdentifier = 123
Floor = 12
Suite = "th"
DistrictCode = 1234
}
$webResult = Invoke-RestMethod -Method POST -Uri $uri -ContentType $contentType -Token $secureToken -Body $Body
write-output $webResult
I can't really remember if you have to convert body to JSON data or if the 'Invoke-RestMethod' does it automatically. In any case: this is how you could do it:
$webResult = Invoke-RestMethod -Method POST -Uri $uri -ContentType $contentType -Token $secureToken -Body ($Body | ConvertTo-Json)
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
I would like to convert a command to generate a masterkey from curl to PowerShell but I never did things like that before..
The objective is to access to an ovea Api but first I have to generate a masterkey.
Here is the curl command (tested on centos and works):
curl -L -H 'Content-Type: application/json' -H 'Authorization: Basic Yw...=' -X POST https://dnsadmin.test.com/apikeys --data '{"description": "masterkey","domains":["testapi.com"], "role": "User"}'
And here is what I tried:
$headers = #{}
$headers.Add = ("Authorization",'Basic Yw...=')
$headers.Add {-description = "masterkey","domains" ["testapi.com"], "role": "User"}
Invoke-RestMethod -Method Post -Uri https://dnsadmin.test.com/apikeys -Headers $headers -ContentType "application/json"
If any of you have an idea I would take it thank you.
When you specify --data with curl it is sent as the body of the request. In the Powershell example you're trying to do some stuff with the header that doesn't make sense.
This would be the equivalent to your curl command:
$headers = #{
Authorization = 'Basic Yw...='
}
Invoke-RestMethod -Method Post -Uri https://dnsadmin.test.com/apikeys -Headers $headers -ContentType "application/json" -Body '{"description": "masterkey","domains":["testapi.com"], "role": "User"}'
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'm trying to rewrite the following curl code: (note I've used OBSCURED to conceal secrets)
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer OBSCURED" \
-X POST \
-d "{\"color\": \"pink\", \"message\": \"OBSCURED\" }" \
https://OBSCURED
into something I can use in Powershell but I'm running into a lot of grief.. this is my attempt so far:
$body = #{
color = "pink"
message = "OBSCURED"
}
Invoke-WebRequest -ContentType application/json -Headers #{"Authorization" = "Bearer OBSCURED"} -Method Post -Body "$body" -Uri https://OBSCURED
But I'm running into this powershell error:
Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed.At E:\BuildAgent\temp\buildTmp\powershell2561816976397359486.ps1:16 char:1
Invoke-WebRequest -Headers #{"Authorization" = "Bearer OBSCURED ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I cannot test this now, but I believe you need to submit JSON as body, so you might try converting your body to JSON first with convertto-JSON and then try posting:
$bodyJSON = body |convertto-JSON
then call:
Invoke-WebRequest -ContentType application/json -Headers #{"Authorization" = "Bearer OBSCURED"} -Method Post -Body $bodyJSON -Uri https://OBSCURED