SOLVED CODE - How to Send a HTTP POST Request, based on a JSON object and Collections via Script Task

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

 

Like 1

Like

Share

2 comments

Hi!

 

Thank you for contacting us! 

 

Could you please provide us with additional screenshots of the issue appeared to understand the case better?

 

Regards, 

Anastasiia

Anastasiia Markina,

Hi Anastasiia,



There is no issue. I struggled with this issue and I solved it.



The above code is my collaboration to the community :)

 

Hope it helps devs with the same issue.

 

Best Regards,

Raz

Show all comments