How do I extract a variable from XML using Postman?

后端 未结 3 509
灰色年华
灰色年华 2021-01-04 02:10

I\'m trying to extract a SessionId from the XML which is returned from a SOAP API.

I\'ve read through the Postman documentation (several times over) but it wasn\'t t

相关标签:
3条回答
  • 2021-01-04 02:39

    Postman v7.20.1

    I'd like to add my answer since above there are a couple of details that took me a while to solve:

    • how to cope with a multipart SOAP response
    • how to manage a JSON object
    • responseBody definition

    Here's the first lines of my XML response to analyze:

    ------=_Part_694527_1470506002.1584708814081
    Content-Type: application/xop+xml;charset=UTF-8;type="text/xml"
    Content-Transfer-Encoding: 8bit
    Content-ID: 
    <e3bd82ac-d88f-49d4-8088-e07ff1c8d407>
        <?xml version="1.0" encoding="UTF-8" ?>
        <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
            <env:Header/>
            <env:Body>
                <ns2:GenericResponse xmlns:ns2="http://XXXXXXXX">
                    <ns2:Service IdcService="XXXXXXXX">
                        <ns2:Document>
                            <ns2:Field name="XXXXXXXX:isSetDefault">1</ns2:Field>
                            <ns2:Field name="XXXXXXXX">CHECKIN_UNIVERSAL</ns2:Field>
    

    After I noticed it was a multipart I've ended up with this Postman Test:

    var response = pm.response.text();
    var responseBody = response.substr(response.indexOf('<env:')); 
    
    pm.test("The body of the response is a valid XML", function () {
         pm.expect(xml2Json(responseBody)).to.exist;
    });
    
    
    pm.test("UCM file upload checkin succesfull", function(){
    
        var responseJson = xml2Json(responseBody);
        var JsonFields = (responseJson['env:Envelope']['env:Body']['ns2:GenericResponse']['ns2:Service']['ns2:Document']['ns2:Field']);
    
        JsonFields.forEach( field => {
        if (field.name == 'StatusMessage'){
            console.log("Field = " + field.name);
            pm.expect(field.text().to.include("Successfully checked in"));
            }
        }); 
    });
    
    0 讨论(0)
  • 2021-01-04 02:52

    Since Postman v7.15.0 the accepted answer did not work for me (it used to before the update). You have to put the path segments into square brackets.

    For example, in the following XML response:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <QuotesPrivateResponse>
        <lease-service>
            <duration-in-months>24</duration-in-months>
        </lease-service>
    </QuotesPrivateResponse>
    

    to retrieve the value duration-in-months:

    var response = xml2Json(responseBody);
    var duration = response["QuotesPrivateResponse"]["lease-service"]["duration-in-months"];
    pm.environment.set("duration", duration);
    
    0 讨论(0)
  • 2021-01-04 02:58

    To extract a variable from XML using Postman, first convert your XML to JSON, using the xml2Json converter method:

    var responseJson = xml2Json(responseBody);
    

    (Where "responseBody" is your xml body.) Then use the console.log method to output your JSON data, as such:

    console.log(responseJson);
    

    Be sure to have followed this tutorial on Enabling Chrome Dev Tools in Postman

    Inside your Test Runner, run the test, then right click and "Inspect" anywhere in the Runner. Select the "Console" tab once Chrome's Dev Tools launch. Expand the "Object" part.

    Then drill-down (expand) until you see the Property whose data you need. Thereafter, set the variable by appending each drill-down level to the parameter you want:

    postman.setGlobalVariable("Session_Id", responseJson.UserSessionToken.SessionID); 
    

    In this case, "responseJson" is the object, "UserSessionToken" was the next level in the drill-down, and SessionId was the parameter I needed within that.

    Note: This answer was the correct solution before v7.15.0 of postman. For versions later than this, the accepted answer may not work.

    0 讨论(0)
提交回复
热议问题