Home > Uncategorized > #Headless CMS using C#, #NodeJS or #Java

#Headless CMS using C#, #NodeJS or #Java

hcms

Headless Content Management – (https://www.headlesscontentmanagement.com/) is a service that decouples the work typically done by content writers and developers. It is designed for use in situations where the content is not going to be viewed on a web-page, but instead is going to be displayed within a mobile app or desktop application.

There are many CMS systems out there, and most of them have APIs. Your content team are probably familiar with the likes of WordPress, Joomla and Dotnetnuke – but most operate on the premise that the content is going to be viewed on a web page via a web browser. Headless Content Management makes no such assumption, and won’t help you display your content online – it just exposes the content via a set of APIs, XML and JSON based.

Headless CMS can also be used in situations where you have an existing website that isn’t based on a CMS platform like WordPress / Joomla etc. So if you have a in-house developed bespoke website, and need to make it easy for content writers to add and edit content on your website. You can use Headless Content Management to manage your content, then have your existing website call our APIs, in order to retrieve the content to be displayed.

The interface with HeadlessContentManagement.com can be done via many different languages, here are three popular examples – C#, NodeJS and Java

C#

// You need to make a service reference to https://HeadlessContentManagement.com/api.asmx
var api = new api.APISoapClient();

// Replace the username and password with those defined on HeadlessContentManagement.com
var strUsername = “demouser@webtropy.com”;
var strPassword = “demo”;

Console.WriteLine(“Select one of the following articles by it’s id number”);
var articles = api.ListArticles(strUsername, strPassword);
foreach(var article in articles)
{
Console.WriteLine(string.Format(“{0}. {1}”, article.id, article.subject));
}
// Ask the user to enter a number
var strId = Console.ReadLine();
var intArticleId = Convert.ToInt32(strId);

// Get that one Article
var selectedArticle = api.ReadArticle(strUsername, strPassword, intArticleId);
Console.WriteLine(selectedArticle.body);
Console.ReadLine();

csharp

NodeJS

var request = require(‘request’); // run npm install request
var email = ‘demouser@webtropy.com’;
var password = ‘demo’;
request.post(
https://www.headlesscontentmanagement.com/ajax/ListArticles.aspx’,
{form:{email:email,password:password}},
function (error, response, body) {
if (!error && response.statusCode == 200) {
showArticleSummary(JSON.parse(body));
}
else
{
console.log(response.statusCode);
console.log(body);
}
}
);

function showArticleSummary(Articles)
{
console.log(“Please Select one of the following article ids”);
for(var i in Articles)
{
var Article = Articles[i];
console.log(Article.id + “. ” + Article.subject);
}
process.stdout.write(“>”)
var stdin = process.openStdin();

stdin.addListener(“data”, function(d) {
var strArticleId = d.toString().trim();
var intArticleId = parseInt(strArticleId);
process.stdin.pause();
ReadArticle(intArticleId);
});
}

function ReadArticle(intArticleId)
{
request.post(
https://www.headlesscontentmanagement.com/ajax/ReadArticle.aspx’,
{form:{email:email,password:password, id:intArticleId}},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(JSON.parse(body).body);
}
else
{
console.log(response.statusCode);
console.log(body);
}
}
);
}

nodejs

Java

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.*;
import org.json.JSONException; // requires org.json package from https://mvnrepository.com/artifact/org.json/json/20170516
import org.json.JSONObject;
import org.json.JSONArray;

public class HeadlessContentManagement {

public static void main(String[] args) {

 

String strJson = getJson(“https://www.headlesscontentmanagement.com/ajax/ListArticles.aspx”, “email=demouser@webtropy.com&password=demo”);

System.out.println(“Got Response”);

JSONArray array = new JSONArray(strJson);

for(int i = 0 ; i < array.length() ; i++){

JSONObject article = array.getJSONObject(i);

System.out.println(article.getInt(“id”) + “. ” + article.getString(“subject”));

}

}

public static String getJson(String serverUrl,String param){

StringBuilder sb = new StringBuilder();

String http = serverUrl;

HttpURLConnection urlConnection = null;
try {
URL url = new URL(http);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod(“POST”);
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout(50000);
urlConnection.setReadTimeout(50000);
urlConnection.connect();
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
out.write(param);
out.close();
int HttpResult = urlConnection.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), “utf-8”));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + “\n”);
}
br.close();
return sb.toString();
} else {
System.out.println( urlConnection.getResponseMessage());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
return null;
}
}

Categories: Uncategorized
  1. August 11, 2018 at 2:30 pm

    This site is now offline. If you are interested in hosting this yourself, leave me a comment, and I will send the source code.

    Like

  1. No trackbacks yet.

Leave a reply to dananos Cancel reply