[SalesForce] Calling a flow through REST api returns “http body is required” error

I'm trying to initiate a flow through the REST api using PHP:

$url = "https://myInstance.salesforce.com/services/data/v40.0/actions/custom/flow/EndCourse_and_Semester_Notification";

$headers = array(
    "Authorization: OAuth myAccessToken",
    "Content-type: application/json",
    "Content-Length: 0"
);

$body = '{}';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($curl);

var_dump($response;
curl_close($curl);

(myInstance replaces my instace and the same goes for the access token)

When i print the response i get the following error message:

string(118) "[{"message":"The HTTP entity body is required, but this request has no entity body.","errorCode":"JSON_PARSER_ERROR"}]"

Why is this happening? i have an empty body and a size defined.
Also worth mentioning is that i tried doing the same without a body or size defined and then i got an error of:

"POST requires content-length"

Best Answer

flow expect input parameters while calling through API so you need to pass blank input array if you are not passing any value. Update your body with following json

{
"inputs": [
    {}
 ]
}

So your code will look like this

$url = "https://myInstance.salesforce.com/services/data/v40.0/actions/custom/flow/EndCourse_and_Semester_Notification";

$headers = array(
    "Authorization: OAuth myAccessToken",
    "Content-type: application/json",
    "Content-Length: 0"
);

$body = '{"inputs":[{}]}';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($curl);

var_dump($response;
curl_close($curl);

enter image description here

Related Topic