1

I am trying to convert curl to powershell with Invoke-RestMethod for onesignal push

the script that is used for onesignal:

curl --include \
     --request POST \
     --header "Content-Type: application/json; charset=utf-8" \
     --header "Authorization: Basic YOUR_REST_API_KEY" \
     --data-binary "{\"app_id\": \"YOUR_APP_ID\",
\"contents\": {\"en\": \"English Message\"},
\"included_segments\": [\"Subscribed Users\"]}" \
     https://onesignal.com/api/v1/notifications

I have tried with following example which I was using for pushover but without success.

   $uri = "https://onesignal.com/api/v1/notifications"
      $parameters = @{
        app_id = 'YOUR_APP_ID'
        contents = "en: English Message"
        included_segments = 'Subscribed Users'
        data = 'foo:bar'
      }
      $parameters | Invoke-RestMethod -Uri $uri -Method Post

I have used this powershell script for pushover which worked fine, but now I want to move to onesignal and I have problems with where/how to put rest api key inside with already using app_id to push messages forward to users.

The code is snatched from: https://documentation.onesignal.com/v5.0/reference#section-example-code-create-notification

I hope that someone can assist me with this problem.

Regards

2 Answers 2

2

Try the following

$basicAuth = "Basic REST_API_KEY";
$headers = @{ Authorization = $basicAuth };
$uri = "https://onesignal.com/api/v1/notifications";
$body = @{ app_id = 'YOUR_APP_ID'; contents = @{ en = 'English Message' }; included_segments = @('Subscribed Users'); data = @{ foo = 'bar' }} | ConvertTo-Json;

Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -ContentType "application/json; charset=utf-8" -Body ([System.Text.Encoding]::UTF8.GetBytes($body));
Sign up to request clarification or add additional context in comments.

Comments

1

I have no idea how this endpoint works but it should work something like this:

$key = "Basic RESTAPIKEY"
$headers = @{}
$headers.Add("Authorization",$Key)
$headers.Add("Content-Type","application/json; charset=utf-8")

$uri = "https://onesignal.com/api/v1/notifications"
$parameters = @{
    app_id = 'YOUR_APP_ID'
    contents = "en: English Message"
    included_segments = 'Subscribed Users'
    data = 'foo:bar'
} | ConvertTo-Json

Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($parameters)) -ContentType "application/json"

If the data-binary part is sent with PowerShell you could also use the -InFile parameter from Invoke-RestMethod.

If just read a little at the link you posted and think the body part should be like the following:

$parameters = @{
    app_id = "5eb5a37e-b458-11e3-ac11-000c2940e62c"
    included_segments = "Array of active users"
    data = @{
        foo = "bar"
    }
    contents = @{
        en = "English Message"
    }
} | ConvertTo-JSON

4 Comments

Well it should be something similar to this? $headers = "Authorization: Basic xxxxxxxx REST API KEY xxxxxxxx" $uri = "onesignal.com/api/v1/notifications" $parameters = @{ app_id = "xxxxxxxxx ONESIGNAL APP ID xxxxxxxxxx" included_segments = "All" data = @{ foo = "bar" } contents = @{ en = "English Message" } } | ConvertTo-JSON Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $parameters -ContentType "application/json" but that doesnt work either, I get 2 keys from onesignal, one for rest api and one for app id.
This is how its done in python: import requests import json header = {"Content-Type": "application/json; charset=utf-8", "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj"} payload = {"app_id": "5eb5a37e-b458-11e3-ac11-000c2940e62c", "included_segments": ["Active Users"], "contents": {"en": "English Message"}} req = requests.post("onesignal.com/api/v1/notifications", headers=header, data=json.dumps(payload)) print(req.status_code, req.reason)
I edited a few bits, maybe you could try again with the edited key, body and headers..
Invoke-RestMethod : The 'Content-Type' header must be modified using the appropriate property or method. Parameter name: name At line:1 char:1 + Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body ([System.Text.E ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.