Hi,
For a few weeks I struggled with sending a HTTP POST Request to a third party application via script task.
I have a collection object that needs to be sent via the web service and eventually catch the response and use the data inside process parameters.
Here you can find the code for doing that using a JSON object.
string x_param = Get("x_process_param");
string y_param = Get("y_process_param");
string z_param = Get("z_process_param");
var ProductLines = Get>("ReadDataUserTask6.ResultCompositeObjectList");
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://url/api/v1/Documents/?token="+x_param);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
int ProductLineIndex = 0;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
using (JsonTextWriter writer = new JsonTextWriter(streamWriter))
{
JObject InvoiceJSon = new JObject (
new JProperty("y",y_param),
new JProperty("z",z_param));
JArray JProductLinesArray = new JArray();
foreach(var ProductLine in ProductLines) {
var ProductName = "";
decimal Qty;
decimal UnitPrice;
ProductLine.TryGetValue("Name", out ProductName);
ProductLine.TryGetValue("Quantity", out Qty);
ProductLine.TryGetValue("Price", out UnitPrice);
JProductLinesArray.Add(new JObject(
new JProperty("ChargeVAT","true"),
new JProperty("Number",++ProductLineIndex),
new JProperty("ProductName",ProductName),
new JProperty("Qty",Qty),
new JProperty("Rate",1),
new JProperty("UnitPrice",UnitPrice),
new JProperty("VATRate",17)));
}
InvoiceJSon.Add(new JProperty("DocumentLines",JProductLinesArray));
InvoiceJSon.WriteTo(writer);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
var LinkToPdf = JObject.Parse(result)["LinkToPdf"].ToString();
var CaspitDocNum = JObject.Parse(result)["Number"].ToString();
Set("DocLinkToPdf",LinkToPdf);
Set("FinanceDocId",DocNum);
}
return true;
Furthermore, I attached a screenshot with the methods that I'm using in the process
Hope it helps.
Have fun :)
Good luck