Javascript runtime error: "SyntaxError: Empty JSON string.

Hi , I have a quick question ,

I am getting Javascript runtime error: "SyntaxError: Empty JSON string.

I have a proxy with GET operation and nothing I am passing in request body.

I checked the below condition in the JS , but still getting Empty Json error at var getPayload.

if((contentType == "application/json" || contentType == "text/xml") && (context.getVariable('request.content')!== null || context.getVariable('request.content')!==""))

{

var getPayload = JSON.parse(context.getVariable("request.content"));

}

Please suggest anything wrong in my null check ?

Solved Solved
0 2 4,172
1 ACCEPTED SOLUTION

You have a logic error.

Your conditional says :

if (A && B) ...

where A is (contentType =="application/json"|| contentType =="text/xml")

and B is (content != null OR content != "")

B will always be true. Try a truth table.

Let's assign the name B1 to the test content != null

and B2 is content != ""

The final value of B is (B1 || B2)

contentB1B2B1 || B2
nullfalsetruetrue
""truefalsetrue
"hello"truetruetrue

In fact there is no value of content for which B1 || B2 resolves to false.

I think you want (content != null AND content != "")

And may I suggest refactoring a bit?

var ctype = context.getVariable('request.headers.content-type');
var content = context.getVariable('request.content');
var allowedContentTypes = ["application/json", "text/xml"];

function isAllowedContentType(ctype) {
  return allowedContentTypes.indexOf(ctype) > -1;
}

if(isAllowedContentType(ctype) && content != null && content != "") {
  var getPayload = JSON.parse(content);
}

There is a second problem - your logic accepts an inbound content-type of "text/xml" and then attempts to perform a JSON.parse(). That's not gonna work.

So really it should be :


var ctype = context.getVariable('request.headers.content-type');
var content = context.getVariable('request.content');
var jsonContentTypeRe = new RegExp("^application/json");

function isJson(ctype) {
  return jsonContentTypeRe.test(ctype);
}

if(isJson(ctype) && content != null && content != "") {
  var getPayload = JSON.parse(content);
  ...
}

View solution in original post

2 REPLIES 2

Any suggestion ? Attached Trace.

7576-trace.png

You have a logic error.

Your conditional says :

if (A && B) ...

where A is (contentType =="application/json"|| contentType =="text/xml")

and B is (content != null OR content != "")

B will always be true. Try a truth table.

Let's assign the name B1 to the test content != null

and B2 is content != ""

The final value of B is (B1 || B2)

contentB1B2B1 || B2
nullfalsetruetrue
""truefalsetrue
"hello"truetruetrue

In fact there is no value of content for which B1 || B2 resolves to false.

I think you want (content != null AND content != "")

And may I suggest refactoring a bit?

var ctype = context.getVariable('request.headers.content-type');
var content = context.getVariable('request.content');
var allowedContentTypes = ["application/json", "text/xml"];

function isAllowedContentType(ctype) {
  return allowedContentTypes.indexOf(ctype) > -1;
}

if(isAllowedContentType(ctype) && content != null && content != "") {
  var getPayload = JSON.parse(content);
}

There is a second problem - your logic accepts an inbound content-type of "text/xml" and then attempts to perform a JSON.parse(). That's not gonna work.

So really it should be :


var ctype = context.getVariable('request.headers.content-type');
var content = context.getVariable('request.content');
var jsonContentTypeRe = new RegExp("^application/json");

function isJson(ctype) {
  return jsonContentTypeRe.test(ctype);
}

if(isJson(ctype) && content != null && content != "") {
  var getPayload = JSON.parse(content);
  ...
}