REST Overview Novel

Hi Lee,

I put this together because I was also curious how the custom methods worked. See below, not if this is the best way, but the first way I figured out how to make it work.

        static void Main(string[] args)
        {
            var client = new RestClient(_serviceUrl + @"/Erp.BO.SalesOrderSvc/GetByID");
            var request = new RestRequest(Method.POST);
            request.AddHeader("authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "shop", "shop"))));
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json", "{orderNum:7045}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            SalesOrderTableset ds = new SalesOrderTableset();

            JObject jo = JObject.Parse(response.Content);
            ds = jo.SelectToken("returnObj", false).ToObject<SalesOrderTableset>();            

            ds.OrderHed[0].PONum = "TEST";
            ds.OrderHed[0].RowMod = "U";

            string data = "{\"ds\":" + JsonConvert.SerializeObject(ds) + "}";

            client = new RestClient(_serviceUrl + @"/Erp.BO.SalesOrderSvc/Update");
            request = new RestRequest(Method.POST);
            request.AddHeader("authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "shop", "shop"))));
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json", data, ParameterType.RequestBody);
            response = client.Execute(request);
        }

EDIT Figured out how to add a new line also (FYI). Probably not the most elegant way, but still playing.

        static void Main(string[] args)
        {
            var client = new RestClient(_serviceUrl + @"/Erp.BO.SalesOrderSvc/GetByID");
            var request = new RestRequest(Method.POST);
            request.AddHeader("authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "shop", "shop"))));
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json", "{orderNum:7045}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            SalesOrderTableset ds = new SalesOrderTableset();

            JObject jo = JObject.Parse(response.Content);
            ds = jo.SelectToken("returnObj", false).ToObject<SalesOrderTableset>();            

            string data = "{\"ds\":" + JsonConvert.SerializeObject(ds) + ",\"orderNum\":7045}";

            // add new line
            client = new RestClient(_serviceUrl + @"/Erp.BO.SalesOrderSvc/GetNewOrderDtl");
            request = new RestRequest(Method.POST);
            request.AddHeader("authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "shop", "shop"))));
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json", data, ParameterType.RequestBody);
            response = client.Execute(request);

            jo = JObject.Parse(response.Content);
            ds = jo.SelectToken("parameters", false).SelectToken("ds").ToObject<SalesOrderTableset>();

            Epicor.SalesOrderSvc2.OrderDtlRow row = ds.OrderDtl.FirstOrDefault(r => r.RowMod.ToUpper() == "A");
            row.PartNum = "test";
            row.LineDesc = "test";

            // update
            data = "{\"ds\":" + JsonConvert.SerializeObject(ds) + "}";
            client = new RestClient(_serviceUrl + @"/Erp.BO.SalesOrderSvc/Update");
            request = new RestRequest(Method.POST);
            request.AddHeader("authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "shop", "shop"))));
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json", data, ParameterType.RequestBody);
            response = client.Execute(request);

        }
1 Like