How do I extract a variable from XML using Postman?

后端 未结 3 555
灰色年华
灰色年华 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题