extract variable

gopaldurgoji
Participant III

Hi All,

In response content im getting the error as shown below.

6440-capture5.png

I want to change only the code field and return the same respone with the new code filed

Eg:

{"propertyId":"B5","errors":[{"code":"CS50061","field":"playerId","message":"Invalid Patron"}]}

TO


{"propertyId":"B5","errors":[{"code":"401","field":"playerId","message":"Invalid Patron"}]}

How can I do it using extract variables.

Solved Solved
0 1 133
1 ACCEPTED SOLUTION

You shouldn't use Extract Variables for that purpose. I wouldn't.

I would use a simple JavaScript Callout to modify that payload.

var c = JSON.parse(context.getVariable('response.content'));
if (c.errors && c.errors[0] && c.errors[0]].code) {
  c.errors[0].code = 401;
  context.setVariable('response.content', JSON.stringify(c, null, 2)+'\n');
}

You might want to add some further logic to check if the errors field is what you expect, and so on.

View solution in original post

1 REPLY 1

You shouldn't use Extract Variables for that purpose. I wouldn't.

I would use a simple JavaScript Callout to modify that payload.

var c = JSON.parse(context.getVariable('response.content'));
if (c.errors && c.errors[0] && c.errors[0]].code) {
  c.errors[0].code = 401;
  context.setVariable('response.content', JSON.stringify(c, null, 2)+'\n');
}

You might want to add some further logic to check if the errors field is what you expect, and so on.