Web RESTful service API calls in Citect?

Is it possible in Citect to make RESTful API calls? For example a PUT command with a JSON payload?

Parents
  • This message is a couple of years old, but I'm kind of in this situation too.  I'd like to be able to do some POSTs with JSON.  I was trying to figure out a way to do it without a custom .NET library and didn't really come up with one.  There are two problems: one is the HTTP connection itself (which could be https), the second is the JSON serialization.  On the HTTP/HTTPS side there may also be headers involved.  Neither of those things seems really practical in the CiCode world.

    The problem I'm having with calling external .NET DLLs is that it seems a little brittle.  I ran some experiments and I had to build exactly for .NET 4.8 and x86 for it to work.  I will show you what I did below.  Like I said, I think it's brittle.  I feel like it's safer to come in the other direction using the API and have an external application.

    This whole mechanism could really stand to have a lot of improvement.  The other thing I really want to do is to be able to publish things via MQTT.  Can't really find a great way to do that yet either.

    using System.Net;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.Serialization.Json;
    using System.IO;

    namespace Squadcast
    {
        public class Incident {
            public string message { get; set; }
            public string description { get; set; }
            public string status { get; set; }
        }

        public class SquadcastClient {
                public string test2() {
                    return "eat me";
                }

                public string test() {
                    var incident = new Incident { message = "This is a test incident", description="This is a test incident generated from .NET", status="triggered" };


                    MemoryStream ms = new MemoryStream();
                    new DataContractJsonSerializer(typeof(Incident)).WriteObject(ms, incident);
                    var text = Encoding.Default.GetString(ms.ToArray());

                    var body = new StringContent(
                            text,
                            Encoding.UTF8,
                            "application/json");

                    var request = new HttpRequestMessage(HttpMethod.Post,
                                            "">api.squadcast.com/.../xxxxxxxxxxxxxx");

                    request.Content = body;

                    var client = new HttpClient();
                    var task = client.SendAsync(request);

                    var waiter = task.GetAwaiter();

                    task.Wait();

                    var response = task.GetAwaiter().GetResult();

                    return "response " + response.StatusCode.ToString()

                    }
        }
    }

Reply
  • This message is a couple of years old, but I'm kind of in this situation too.  I'd like to be able to do some POSTs with JSON.  I was trying to figure out a way to do it without a custom .NET library and didn't really come up with one.  There are two problems: one is the HTTP connection itself (which could be https), the second is the JSON serialization.  On the HTTP/HTTPS side there may also be headers involved.  Neither of those things seems really practical in the CiCode world.

    The problem I'm having with calling external .NET DLLs is that it seems a little brittle.  I ran some experiments and I had to build exactly for .NET 4.8 and x86 for it to work.  I will show you what I did below.  Like I said, I think it's brittle.  I feel like it's safer to come in the other direction using the API and have an external application.

    This whole mechanism could really stand to have a lot of improvement.  The other thing I really want to do is to be able to publish things via MQTT.  Can't really find a great way to do that yet either.

    using System.Net;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.Serialization.Json;
    using System.IO;

    namespace Squadcast
    {
        public class Incident {
            public string message { get; set; }
            public string description { get; set; }
            public string status { get; set; }
        }

        public class SquadcastClient {
                public string test2() {
                    return "eat me";
                }

                public string test() {
                    var incident = new Incident { message = "This is a test incident", description="This is a test incident generated from .NET", status="triggered" };


                    MemoryStream ms = new MemoryStream();
                    new DataContractJsonSerializer(typeof(Incident)).WriteObject(ms, incident);
                    var text = Encoding.Default.GetString(ms.ToArray());

                    var body = new StringContent(
                            text,
                            Encoding.UTF8,
                            "application/json");

                    var request = new HttpRequestMessage(HttpMethod.Post,
                                            "">api.squadcast.com/.../xxxxxxxxxxxxxx");

                    request.Content = body;

                    var client = new HttpClient();
                    var task = client.SendAsync(request);

                    var waiter = task.GetAwaiter();

                    task.Wait();

                    var response = task.GetAwaiter().GetResult();

                    return "response " + response.StatusCode.ToString()

                    }
        }
    }

Children
No Data