Json error when calling a script via API

Hello, we’ve made a major version upgrade (from 4.3.4 to 5.5.2), and everything went well, but we’re encountering a blocking problem. when we make an API call to launch a script

We use POST /api/v1/script/{id} to launch our scripts, so if the script has no parameters, it works fine without a problem.

But if it does have parameters, we’re obliged to put the parameters in the body, in which case we get the following error:

Data at the root level is invalid. Line 1, position 1.

curl -X 'POST' \
  'https://xxxxxxxxxxx/api/v1/script/xx' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
"jobParameters": [
    {
      "name": "GroupName",
      "Type": "System.String",
      "Value": "ADG-Toto"
    },
    {
      "name": "Action",
      "Type": "System.String",
      "Value": "Create"
    }
  ]
}'

I’ve also tested this method: the api call works without a problem, but the variables appear empty in the job.

curl -X 'POST' \
  'https://xxxxxxxxxxx/api/v1/script/xx' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
"jobParameters": [
    {
      "name": "GroupName",
      "Type": "System.String",
      "stringValue": "ADG-Toto"
    },
    {
      "name": "Action",
      "Type": "System.String",
      "stringValue": "Create"
    }
  ]
}'

I’m reproducing the problem with a new instance.
Is there a change I’ve missed?

Regards
Nicolas

Product: PowerShell Universal
Version: 5.5.2

Hello everyone,
Anyone have a problem running a script with API parameters?

I’ll admit I only have one endpoint currently, I’m not super versed on it, and I put my processing code in the endpoint code and not another script, but I here’s what I have done.

For my API, it is setup as a GET and I have two parameters I pass as the query string. This is at the top of my endpoint code:

param(
        $requestId,
        $approval
    )

# functions and code to process here

Then I can call the endpoint with:
https://psu.domain.com/endpoint?requestId=12341234&approval=approved
And my endpoint code uses those values as $requestId and $approval and I then process them as needed.

Again, I haven’t done this specifically with an API, but I expect you could call a script and pass parameters like this (I used this to call scripts in an app page):

# Get the script reference
$Script = Get-PSUScript -Name 'Some-ScriptName.ps1' -TrustCertificate

# Invoke the script and get the job
$Job = Invoke-PSUScript -Script $Script -Parameter1 $requestId -Parameter2 $approval

if ($Job) {
    # Wait for the job to complete
    $Job | Wait-PSUJob

    # Do stuff with the output or after the script is done
}

I don’t imagine this will solve your stated problem, but hopefully it might spark some thoughts or ideas.

1 Like

Thanks for your reply :slight_smile:

I would have liked to use the native API functions without putting all my parameters in the URL. Because if I’m not mistaken, we’re limited to a certain number of characters in a URL and we can have a parameter with, for example, a hundred e-mail addresses to pass (in the same parameter, separated by commas).

As for the Powershell Universal modules, they’re not possible in our way of working, as they’re Python scripts that launch API calls.