Hi there! I thought I’d share a (possibly!) useful snippet of code that you can use to receive REST requests, and respond with a canned body. It’s in Node. I don’t know Node, so apologies if it’s not glamourous code. I was inspired by the “Simple REST Consumer” on this tutorial: Publish / Subscribe
Update the variable RC_HOST
at the top of the file with your machine’s IP address. Don’t use localhost
, use the actual IP address. Note, if using WSL2, you’ll need to use the “internal” address of the virtual machine. On my Windows machine, I run this from the Ubuntu shell, so my IP address looks like 172.28.123.115
.
And if running the PubSub+ broker locally, inside your RDP Rest Consumer, you can’t use “localhost” or “127.0.0.1” when testing with an RDP, because the Solace broker has it’s own networking setup. You’ll have to specify your actual IP address, or whatever you started the server with.
Anyhow, copy this into a file, and then simply run node NodeServer.js
% cat NodeServer.js
var http = require('http');
var RC_PORT = 9090;
//var RC_HOST = '127.0.0.1'; // only works from browser, not RDP
//var RC_HOST = '10.1.1.245'; // home
var RC_HOST = '192.168.42.194'; // office LAN
http.createServer(function (req, res) {
let date_ob = new Date();
var dateStr = date_ob.getHours()+':'+date_ob.getMinutes()+':'+date_ob.getSeconds();
//console.log(req); // all the details of this request
console.log('Received message: ' + req.method + " " + req.url + ' at ' + dateStr);
Object.keys(req.headers).forEach(function(key) {
//if (!key.startsWith('solace')) return;
var val = req.headers[key];
console.log(' HEAD:' + key + ' = ' + val);
});
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
// at this point, `body` has the entire request body stored in it as a string
console.log(' BODY: '+ body);
});
// RESPONSE TIME!
//res.writeHead(200); // bytes message
res.writeHead(200, { 'Content-Type': 'text/plain' }); // text message
res.write("Hello world from Aaron's test HTTP server!");
res.end();
//console.log(res);
}).listen(RC_PORT,RC_HOST);
// good to go!
console.log('Server running at http://'+RC_HOST+':'+RC_PORT+'/');
When you send a request, it will dump stuff out to the console. Here’s a quick test from Chrome, loading http://192.168.42.194:9090/hello/world
:
Server running at http://192.168.42.194:9090/
Received message: GET /hello/world at 15:0:48
HEAD:host = 192.168.42.194:9090
HEAD:connection = keep-alive
HEAD:upgrade-insecure-requests = 1
HEAD:user-agent = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
HEAD:accept = text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
HEAD:accept-encoding = gzip, deflate
HEAD:accept-language = en-GB,en-US;q=0.9,en;q=0.8
BODY:
Have fun playing with RDPs and Solace+REST!