Calling Function from Rest and reading Input parameter

Hi All

Hoping someone can assist me here. We have companies in separate servers where I need to trigger a Function in Company B from a BPM in Company A. I am doing this through Rest API and I can successfully trigger the Function. However, the input parameter to the Function is not being read.

The function accepts dsObject (Type String) as input parameter which is a dataset converted to a string, the function currently just returns an out put parameter which is just reading the input parameter which currently comes through as null.

Code:

var client = new HttpClient();
var url = "https://ServerAddress/Environment/api/v2/efx/Company/Library/Function";
string username = "User";
string password = "Password";
var creds = $"{username}:{password}";
var credsBase64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(creds));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credsBase64);

client.DefaultRequestHeaders.Add("X-API-Key", "a*******************************");

//Converting dataset - dsResults to string
string jsonString = JsonConvert.SerializeObject(dsResults, Formatting.Indented);

//Setting string equal to dsObject which is how the input parameter is set in the Function
var jsonBody = $"{{ \"dsObject\": \"{jsonString}\" }}

//Rest Call
var response = client.PostAsync(url, new StringContent(jsonBody, Encoding.UTF8, "application/json")).GetAwaiter().GetResult();

//Reading output:
if(response.StatusCode.ToString() == "OK")
{
	this.PublishInfoMessage("Output: " + root["outputValue"].ToString() , Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "FirstVar","SecondVar"); 
}

Does anyone on know how I should be passing the body in the Rest call to be able to set it to the input parameter on the function?

Your jsonBody is messed up. We could fix it in string land, but having a hard time typing that on my phone.

var objectToSend = new { dsObject = jsonString }; 

Now serialize that as jsonBody instead and send that.

2 Likes

Unless your code didn’t come over right. Always use the backticks to format the code properly.

2 Likes

As an alternative to Http Client

// Assemblies Required
RestSharp.dll

// Usings
using RestSharp;
using System.Text.RegularExpressions;

var client = new RestClient(integrationUrl);
    var request = new RestRequest(Method.POST);

    request.AddHeader("Content-Type", "application/json");

    // JSON Body
    var payload = new
    {
        jobNum = jobAsmRow.JobNum,
        asm = jobAsmRow.AssemblySeq,
        oprSeq = 0
    };

    request.AddJsonBody(payload);

    IRestResponse response = client.Execute(request);

    if (response.IsSuccessful)
    {
        // OkK
    }
3 Likes

Thanks a million, this worked perfectly :grinning_face:

1 Like