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! Go to 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)
content | B1 | B2 | B1 || B2 |
null | false | true | true |
"" | true | false | true |
"hello" | true | true | true |
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); ... }
Any suggestion ? Attached Trace.
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)
content | B1 | B2 | B1 || B2 |
null | false | true | true |
"" | true | false | true |
"hello" | true | true | true |
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); ... }
User | Count |
---|---|
3 | |
2 | |
1 | |
1 | |
1 |