I’m trying to transform the JSON body of the request into a string formatted as 'key1=value1&key2=value2&key3=value3'. I have a regular JavaScript line that works great:
If I use it in an online Javascript sandbox, the result is perfect. If I try this in Postman, it interprets every character and not the key/value pair, even if I do JSON.parse(params) - so if the input is { "ba": "sheep", "have": "wool" }
the output is 0={&1="&3=b&4=a&5=" - you get the idea.
in this case, the output should be ba=sheep&have=wool
How can I get Postman to treat it as key/value pairs instead of a string?
This is in the pre-request script at the collection level.
This feels more like a StackOverflow question about Javascript than something specifically to do with Postman. I was able to find a solution on SO and get it working in the pre-request script.
I’m sure that there are more efficient solutions and I’m not sure if this will still work on more complex request payloads but hopefully this helps.
Thanks. I was very close, but I missed a few things. I appreciate the help.
I thought about SO, but I figured this form would be better for Postman specific-questions. SO wouldn’t know about pm.request.etc and the quirks of Postman, whatever they are. Thanks again for the quick response.
BTW, is there a way to debug/step through these scripts instead of just running them and trying to guess what wasn’t correct?
In terms of debugging your scripts, you have the Postman Console available to you, where you can use console.log() statements to inspect various parts of the script while it’s being executed.
The sandbox doesn’t have the ability to attach a debugger and step through the lines of code.