Hey, I am preparing and return the xml format in R...
# general
j
Hey, I am preparing and return the xml format in Restlet, however, it is displaying as a text. can anyone have an idea? How to prepare a xml to send as a response? will netsuite support xml response?
l
well xml is text, so as json. on the other end, the endpoint parses and consumes the data.
why use xml, it's heavier payload vs json.
j
@leo_ns My user requested the XML, since they are using another api using xml
l
that's fine. they need to see the returned xml text, then be able to parse, convert to object, whatever. it's just text, no rocket science https://www.w3schools.com/xml/note.xml
j
@leo_ns Here I am preparing the xml, however, it is returning a text. ex: <xml><id>1</id></xml> final result is : 1
l
https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4344917661.html#N%2Fxml-Module unverified code
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */

define(['N/xml'], function(xml) {

    function getPersonData() {
        // Define person data
        var personData = {
            id: 1,
            name: "Jayaram"
        };

        // Create XML document
        var xmlDoc = xml.createDocument();
        var rootNode = xmlDoc.createElement({
            name: 'person'
        });

        // Create child elements for person data
        var idNode = xmlDoc.createElement({
            name: 'id'
        }).textContent = personData.id;

        var nameNode = xmlDoc.createElement({
            name: 'name'
        }).textContent = personData.name;

        // Append child elements to the root node
        rootNode.appendChild(idNode);
        rootNode.appendChild(nameNode);

        xmlDoc.appendChild(rootNode);

        // Serialize XML document to string
        var xmlString = xmlDoc.toString();

        return xmlString;
    }

    function doGet() {
        return getPersonData();
    }

    return {
        get: doGet
    };

});
Copy code
<person>
    <id>1</id>
    <name>Jayaram</name>
</person>
j
@leo_ns Thanks for your info. However, xml module doesn't have createDocument() function. can i know from where you found this code?
l
🤣 chatgpt. famously making sh*t up on the fly. sorry
j
oh.No problem, chatgpt wont give proper information.
l
https://blog.prolecto.com/2020/09/06/the-easy-way-to-generate-netsuite-xml-files/ according to this "There is no easy native way to generate XML (or JSON files) in NetSuite"
it does show you how.
j
Except restlet, remaining all script xml is supporting
Once again, thanks for your help.
l
i heard others say you can upload external javascript libraries in the file cab. then in your code, reference it. I can do light scripting. not a hardcore coder.
j
my concern , even we can use external library, i am thinking will it return xml.
e
If you know you are sending xml and want your response as xml, make sure your content type in your request header is the same, ie. text/xml or application/xml.
j
@Emanuel V I tried, its not working on restlet var xmlStr = '?xml version="1.0" encoding="utf-8" ?'; xmlStr+='internalid123/internalid' xmlStr+='/xml' var headers = {}; headers['Content-Type'] = 'application/xml'; return { body: xmlStr, headers: headers };