Siege with JSON POST data / Load Testing HTTP POST API with Siege & JSON DATA

Reminder:

Received the following error while trying to use siege to load test a simple API and validate the request body.

This error message was thrown by the body-parser module in nodejs.

SyntaxError: Unexpected token ' in JSON at position 1

To POST JSON to siege it seems you have two options for passing the JSON data on the command line:

1) use single quotes for all your parameters passed to the siege executable and double quotes on your JSON data inside these parameters.
OR
2) escape the quotation marks on the data being sent…with a backslash “\” and use double quotes consistently.

 

A successful request looks like the following line of code:


## THIS WORKS USING \ to escape quotations
siege -c2 -t30S "http://localhost:3000/register POST {\"contactName\":\"John DaCosta\"}" --content-type "application/json"

## THIS ALSO WORKS use single quotes for main calls to siege, and double quotes inside our single quotes...
siege -c2 -t30S 'http://localhost:3000/register POST {"contactName":"John DaCosta"}' --content-type 'application/json'

## THIS DOES NOT WORK --> SyntaxError: Unexpected token ' in JSON at position 1
siege -c2 -t30S "http://localhost:3000/register POST {'contactName':'John DaCosta'}" --content-type "application/json"

To review the data on the console in node.js

Setup the Application: 

npm init

# install express and body parser for handling requests./ running dev server.
npm install --save express body-parser

# install nodemon globally - automatically runs app each time code changes.
npm install -g nodemon

# create the file for our application code
touch app.js

Save the following code as app.js
const express = require('express')
const app = express()
const port = 3000
var bodyParser = require('body-parser')
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({
extended:false
})

var requestCounter = 0;
app.get('/', (req, res) => res.send('Hello World!'))
app.post('/register', jsonParser, (req, res) => {
requestCounter+=1;
var_errors= [];
var_responseData= {};
console.log("Received Post: "+requestCounter.toString())
//console.log("Req.params: " + JSON.stringify(req.params.email));
//console.log("req.body: " + JSON.stringify(req.body));
var email;
var phoneNumber;

try {
email=req.body.email;
_responseData.email=email;
} catch {

}

try {
phoneNumber=req.body.phoneNumber;
_responseData.phoneNumber=phoneNumber;
} catch {
}

if (email===undefined||email==null) {
_errors.push('Email Address Not Provided');
}

if (phoneNumber===undefined||phoneNumber==null) {
_errors.push('Phone Number Not Provided')
}

var responseBody= {
err:_errors,
data:_responseData
};

console.log(JSON.stringify(responseBody));
//res.send('Register Posted');
res.json(responseBody);
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Run your web app upon each change via:

nodemon app.js