Solutons Lounge

How to transform JSON body to key=value&key2=value2 – πŸ—£ General Discussion


Hi All,

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:

const bodySignParams = Object.entries(params).map(([key, val]) => key + β€˜=’ + val).join(β€˜&’);

where β€˜params’ is a JSON object

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.



Source link

Exit mobile version