This blog post presents a prototype for a .NET class that invokes HTTPS service APIs in the Contentstack SaaS headless CMS that accept and return JSON. This is just a short post to publish the sample code that I intend to reference from one or more additional blog posts.
Contentstack provides a .NET SDK that wraps its HTTPS content delivery APIs, but not its content management APIs. As the content delivery SDK restricts access to various classes and uses delivery tokens rather than management tokens for security, I found it easier to write a new implementation to invoke content management services than to extend the content delivery SDK with the same functionality. I refactored some code from the SDK to create a class that invokes Contentstack HTTPS services.
To demonstrate functionality simply and explicitly, the sample code is intentionally a single procedure with minimal functionality (no passing of query string or HTTP header parameters), vendor-specific (Contentstack) hard-coding (connection configuration), and setting minimal HTTP headers rather than being object-oriented, feature-rich, reusable, and refactored for maintainability. This might assist others attempting to access HTTPS services with .NET or Contentstack services from other platforms.
Below is an example that uses this class to invoke a Contentstack content management API that updates a field value (Content Management API Docs | Contentstack) with hard-coding of the field name and the URL of the service that retrieves entries associated with the “example” content type and an assumption that uid and value contains the identifier of the entry and the new value for the field.
string url = "https://api.contentstack.io/v3/content_types/example/entries/" + uid; JObject postBody = new JObject( new JProperty("entry", new JObject( new JProperty("fieldname", value)))); string result = await new HttpRequestHandler().ProcessRequest( url, postBody);
The result contains the JSON payload of the response in case the caller wants to use System.Text.Json rather than Newtonsoft.JSON (my preference). Note that the full request context including HTTP headers is not available to callers and the method contains no error management.
See also:
With comments, I think the code is relatively self-explanatory, but please comment if you see defects or have questions or suggestions.
2 thoughts on “Prototype .NET Class to Invoke [Contentstack] [SaaS] [Headless] [Content Management] HTTPS JSON Service APIs”