Hopefully this will be helpful to someone else.
We needed to send a http post with a content-type of application/json and parse a JSON string from the http response. I used the .Net Code Execute task to make the call and to parse the JSON returned. Below is the code generalized to allow for easy customization. Be sure to include a reference to System.Runtime.Serialization.dll and System.Web.Extensions.dll. Here is a links to a MSDN article on
JSON serialization
using System;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;
public class Test
{
public class YourClass
{
public string someValue= "";
public string otherValue = "";
public string differentValue = "";
}
public static string GetKey()
{
var yc = new YourClass();
string requestBody = "{\"someValue\": \"this is some data\",\"otherValue\": \"other data appears here\",\"differentValue\": \"different data \"}";
string endpoint = "https://someendpoint/api";
// set up the Http request
var yourHttpRequest = (HttpWebRequest)WebRequest.Create(endpoint);
yourHttpRequest.ContentType = "application/json";
yourHttpRequest.Method = "POST";
// write the body of the http request
using (var sw = new StreamWriter(yourHttpRequest.GetRequestStream()))
{
sw.Write(requestBody);
sw.Flush();
sw.Close();
}
// get and parse the response
using (var yourHttpResponse = (HttpWebResponse)yourHttpRequest.GetResponse()) {
// parse the json returned
using (var reader = new StreamReader(yourHttpResponse.GetResponseStream())){
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
yc = js.Deserialize<YourClass>(objText);
}
}
return yc.someValue;
}
}
Edited by moderator
2017-09-29T15:05:53Z
|
Reason: Not specified