Try PubSub+

Quick-and-dirty REST server for RDP testing

AaronAaron Member, Moderator, Employee Posts: 133 Solace Employee

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: https://solace.com/samples/solace-samples-rest-messaging/publish-subscribe/

On my Windows machine, I run this from the Ubuntu shell in WSL. Just make sure you change the IP address variable to your actual IP address. You can't use "localhost" or "127.0.0.1" when testing with an RDP (even though it will respond to your local web browser), because the Solace broker has it's own networking setup.

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!

Sign In or Register to comment.