C
C
Clementino2021-03-26 12:22:14
API
Clementino, 2021-03-26 12:22:14

How to save token from response xml to variable, in postman?

I use postman to access the api, first I make a POST request for authorization, in the response there is a token that I want to save to an environment variable (so that I can use it for authorization in other requests).
An answer like this:

<?xml version='1.0' encoding='UTF-8'?>
<Response xmlns="http://site.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://site.com/api http://site.com/api/api.xsd">
    <credentials token="12345678">
    </credentials>
</Response>


If I understood everything correctly, then this can be done on the Tests tab of the same authorization request.
I write the following code there, I took it from the official blog ), but nothing happens, postman just writes: There are no tests for this request. I don't understand what I'm doing wrong.
var responseJson = xml2Json(responseBody);
var token = responseJson["Response"]["credentials token"];
postman.setEnvironmentVariable("token", token);


ps The environment and the token variable are created in it. Also, this environment is selected active on the top right of the postman window.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Clementino, 2021-03-26
@Clementino

It turned out that the matter was in the format of parsing xml with the xml2Json function, the format is rather strange at the output, and the correct access to the token in this example looks like this:

var token = jsonObject["tsResponse"]["credentials"]["$"]["token"];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question