Solace REST Example Code

The following sections provide some example code for sending and receiving REST messages.

Sending Messages to Solace Using CURL

Sending a message to a topic “a” (both commands are equivalent):

curl -X POST -d "Hello World Topic" http://<ip:port>/a --header "Content-Type: text/plain"
curl -X POST -d "Hello World Topic" http://<ip:port>/TOPIC/a --header "Content-Type: text/plain"

Sending a message to a queue “Q/test”:

curl -X POST -d "Hello World Queue" http://<ip:port>/QUEUE/Q/test --header "Content-Type: text/plain"

Request/Reply to Solace on topic “rr” with timeout of 30 seconds and Solace Correlation ID 'x':

curl -X POST -d "Hello World Request Reply" http://<ip:port>/TOPIC/rr --header "Solace-Reply-Wait-Time-In-ms:30000" --header "Solace-Correlation-ID:x"

Sending Messages to Solace Using JavaScript

The following is a sample of how to send a message to topic “a/b/c” using JavaScript and Node.js:

var http = require('http');
 
var userString = "Hello World JavaScript";
 
var headers = {
  'Content-Type': 'text/plain',
  'Content-Length': userString.length
};
 
var options = {
  host: '<HOST>',
  port: <PORT>,
  path: '/TOPIC/a/b/c',
  method: 'POST',
  headers: headers
};
 
 
// Setup the request.  The options parameter is
// the object we defined above.
var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});
 
req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});
 
req.write(userString);
req.end();

<HOST> and <PORT> must be updated to point to the event broker.

Receiving Messages from Solace Using JavaScript

The following code is a minimalist HTTP server that can be run using Node.js. It will respond to all incoming requests with 200 OK and “Hello World Response” as the body of the HTTP response.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': ‘text/plain'});
  res.end('Hello World Response\n');
}).listen(<PORT>, ‘<HOST>');
console.log('Server running at http://<HOST>:<PORT>/');

When configuring the queue binding to this server, you must use a valid path, for example “/”, for the POST request target.