BigCommerce API Newbie

Hello everyone, I have been trying to create a post request in BigCommerce through a BPM. At first, I tried classic custom code, and that didn’t seem to work as I couldn’t use RestSharp there (not sure why, but I’m interested to know if someone has the time). After implementing a suggestion from @Dragos, I built my BPM accordingly pulling in RestSharp. My successfully compiled code:

var client = new RestClient("https://api.bigcommerce.com/stores/[Kmart]/v3/carts?include=redirect_urls");
var request = new RestRequest("/stores/[Kmart]/v3/carts?include=redirect_urls", Method.Post);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("X-Auth-Token", "FriedChickenIsYummy");
var body = @"yadaYada";
request.AddStringBody(body, DataFormat.Json);
var response = client.Execute(request);
Console.WriteLine(response.Content);

When I save the BPM though, I get this error:
Update.Pre.BigCommerce.cs(144,91): error CS0117: ‘Method’ does not contain a definition for ‘Post’
Update.Pre.BigCommerce.cs(181,9): error CS1061: ‘RestRequest’ does not contain a definition for ‘AddStringBody’ and no accessible extension method ‘AddStringBody’ accepting a first argument of type ‘RestRequest’ could be found (are you missing a using directive or an assembly reference?)

Thanks for you patience and help in this. I feel like I’m missing something obvious.

I would first check if that bigcommerce URL supports the POST method. Then, that AddStringBody - look at Newtonsoft for JSON formats. You can create your own class or use the lazy way with JObject and JArray. Below partial example for sales orders:

// create JSON
dynamic objTCall = new Newtonsoft.Json.Linq.JObject();
dynamic objOrderArray = new Newtonsoft.Json.Linq.JArray();
dynamic objOrder = new Newtonsoft.Json.Linq.JObject();

// sales order
objOrder = new Newtonsoft.Json.Linq.JObject();
objOrder.OrderNo = "order no here";
objOrder.OtherFields = "...";

// add order to array
objOrderArray.Add(objOrder);

// add array to call object
objTCall.orders = objOrderArray;

// serialize
string sbdy = Newtonsoft.Json.JsonConvert.SerializeObject(objTCall); 

// REST CALL
client = new RestClient(rpURL);
client.Timeout = -1;
request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", rpToken);
request.AddParameter("application/json", sbdy,  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

I’m using Epicor functions with the following custom code:

WebRequest request = WebRequest.Create (SFURL+"pricelist");
var username   = SFUser ;
var password   = SFPassWord ;
string encoded = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1")
                               .GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
request.Headers.Add("client_id", SFClientID);
request.Headers.Add("client_secret", SFClientSc);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string oResult = Newtonsoft.Json.JsonConvert.SerializeObject(tsRetBAQ);
NoRecs = tsRetBAQ.Tables[0].Rows.Count;
payLoad = oResult;


byte[] byteArray = Encoding.UTF8.GetBytes (oResult);

// Set the ContentType property of the WebRequest.
request.ContentType = "application/json; charset=utf-8";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();

The code has the following using

using System.Net;
using System.Text;
using System.IO;
using System.Text.Json;
using Newtonsoft.Json;
1 Like

Thank you guys for your help in this. I was able to get this to work. My code is below. One final question if anyone would be so kind to answer. I had to download/install RestSharp to the client obviously. Do I have to do this to each machine that will use this BPM? Is this the main reason why you should always use Epicor Functions Maint? Should I use Functions Maint and get RestSharp on the server? Should you never use RestSharp? Trying to understand the best practice.

var client = new RestClient("https://api.bigcommerce.com");
var request = new RestRequest ("/stores/[Kmart]/v3/carts?include=redirect_urls");
request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("X-Auth-Token", "FriedChickenIsYummy");
var body = @"yadaYada";
request.AddJsonBody(body);
var response = client.Execute(request);
this.PublishInfoMessage(response.Content, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");

Also, for anyone else that runs into this. Despite this working 100%, when it’s ran, if you check the syntax in the custom code editor in directives maint, you’ll get this error:
image
Despite that, I was still able to save and run the BPM just fine. This very thing wasted a lot of time for me; I’m hoping this will help someone else avoid that.