How to convert application/x-www-form-urlencoded to JSON using javascript

Hi, I  just want to have an option to apply JSON validation in application/x-www-form-urlencoded requests. Thanks.

Solved Solved
0 2 3,811
1 ACCEPTED SOLUTION

Hi Dino,

I already find the answer about this question. Thanks for attending on our questions.

Here is what I found and it works. Hope it helps others also. It applies also on query parameters. Just replace "request.formstring" with "request.querystring".

var formRequest = context.getVariable("request.formstring").split("&");
formRequest.forEach(function(keyValuePair) {
	var nameAndValue = nameValuePair.split('=');
	jsonOutput += '"' + nameAndValue[0] + '"' + ':' + '"' + nameAndValue[1] + '"' + ',';
});
Output = '{' + Output.substr(0, Output.length -1) + '}';
context.setVariable('request.content', Output);

 

 

View solution in original post

2 REPLIES 2

Hi can you elaborate a little on your question by giving an example or two?  Show an example of an inbound form, with various parameters, then explain what it is you want to do with that form, and how the JSON part is involved?  Is JSON a requirement, or ??? Is the main thing to validate the form params?  Elaborate a little, please.

Hi Dino,

I already find the answer about this question. Thanks for attending on our questions.

Here is what I found and it works. Hope it helps others also. It applies also on query parameters. Just replace "request.formstring" with "request.querystring".

var formRequest = context.getVariable("request.formstring").split("&");
formRequest.forEach(function(keyValuePair) {
	var nameAndValue = nameValuePair.split('=');
	jsonOutput += '"' + nameAndValue[0] + '"' + ':' + '"' + nameAndValue[1] + '"' + ',';
});
Output = '{' + Output.substr(0, Output.length -1) + '}';
context.setVariable('request.content', Output);