Anyone know how to get around the request by microsoft to add '\' to a REST POST?

ChrisRowland1
Tera Expert

I have a working REST POST Message with a variable body = {"teamId":"{999999-9999-999999-999999}","existingUsersJson":"[ \"999999-9999-999999-999999\", \"999999-9999-999999-999999\"]"}

 

From the TEST in the rest message I get a 201 success, but when I am trying to use the rest message in a script, the system keeps escaping the '\' characters.

 

Ideally I would like to pass a variable team_id to r.setStringParameterNoEscape('body', team_id) but this removes the ‘\’ when setting the variable.

 

Anyone know how to set the variable string to pass while retaining the '\' characters?

 

This is the microsoft article that requires this format:

https://developercommunity.visualstudio.com/t/how-to-add-team-administrator-through-azure-devops/821...

 

1 ACCEPTED SOLUTION

Thank you Tony.  The resolution to this was create the body on the rest message record, and pass my id as a paramter.

This worked using the suggested script preview in the post message.

View solution in original post

2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, you could try escaping \ with another \ IE \\ but looking at your code the issue seems to be that the result does not have consistent string " formatting, so the characters within the array elements are not necessarily interpreted as expected and I suspect the code is just making the most of what it has.

I am not sure how MS manages to decode this properly, but perhaps they stringify and parse?
Can you assemble your body in a way that allows you to encapsulate the data correctly? perhaps something like this

 - still not sure if it run as expected, but I think it makes the issue clearer

var body = {};
body.teamId = '{999999-9999-999999-999999}';
body.existingUsersJson = '[ \"999999-9999-999999-999999\", \"999999-9999-999999-999999\"]';

gs.info(body.teamId);
gs.info(body.existingUsersJson); // the result of this is not what you'd expect, and a bit surpising the the first " of the array elements are after the \ rather than encapsulating it

var str = JSON.stringify(body);
gs.info(str);

 

 

 

Thank you Tony.  The resolution to this was create the body on the rest message record, and pass my id as a paramter.

This worked using the suggested script preview in the post message.