This topic contains 7 replies, has 0 voices, and was last updated by ironside 7 years, 10 months ago.

  • Author
    Posts
  • #5655

    nic kcd

    I have been banging my head on this for a while and I am sure it is something stupid.

    I have a RESTlet that takes JSON. It parses out the JSON, generates some XML then it calls a third party API. No matter what I do I keep on returning the XML in a string that is automatically escaping the quotation marks with backslash ( “) and then is is not allowing me to resolve the URL with the XML. My theory is that because the RESTlet is accepting JSON is wrapping it is quotation marks and escaping the quotes. Or maybe this is just something that postman does.

    Things I have tried.Using single quotes around string and double quotes for the XMLattributes var xmlRequest = ‘‘returns “

    Using double quotes and escaping quotes inside with backslashsesattributes var xmlRequest = “”returns “

    Using nlapiStringToXMLErrors out when I try to call this on the final string
    Errors out saying the XML is not completed when I try to call it on everyline

    Using DOMParserI guess it is not touching DOM so doesn’t work

    Using nlapiEscapeXMLEscapes everything and turns it to jibberish

    I even tried string.replace the ” for ” lol

    I will post the code, but I am sure the issue is that it is passing over that escaped quote, because I can take the xmlRequest, clean by removing escaped quote with regular quotes that up and use the exact string to make a successful call to the third party API.

    PHP Code:

    function postRequest(request) {

    var parsed = request;
    var key = [‘item’,’qty’, ‘units’,’paint_eligible’, ‘weight’];
    var resultsReduce = new Object();

    var headers = new Array();
    headers[‘Content-Type’] = ‘application/xml’;
    headers[‘Accept’] = ‘application/xml’

    key.forEach(function(item){
    resultsReduce[item] = parsed.freight.items.reduce(function(array, object) {
    return array.concat(object[item]);
    }, []);
    });

    var totalWeight = parsed.freight.items.reduce(function(array, object) {
    return array += object.weight * object.qty
    }, 0);

    // build out XML
    var xmlRequest = ‘
    xmlRequest += ”
    xmlRequest += ‘4354534’
    xmlRequest += ‘BillingRate’
    xmlRequest += ”
    xmlRequest +=     ”
    xmlRequest +=        ”
    xmlRequest +=    ”
    xmlRequest +=    ‘BlanketCost’
    xmlRequest +=    ”
    xmlRequest +=        ‘Cost’
    xmlRequest +=        ‘All’
    xmlRequest +=        ‘TotalAmount’
    xmlRequest +=        ‘false’
    xmlRequest +=    ”
    xmlRequest += ”
    xmlRequest += ” + ‘2016-12-24T09:29:40’ + ”     //parsed.pickup_date
    xmlRequest +=”
    xmlRequest +=    ”
    xmlRequest +=        ‘1’
    xmlRequest +=        ”
    xmlRequest +=            ‘GIBSONIA’
    xmlRequest +=            ”
    xmlRequest +=                ‘PA
    xmlRequest +=            ”
    xmlRequest +=            ‘15044’
    xmlRequest +=            ”
    xmlRequest +=                ‘US’
    xmlRequest +=            ”
    xmlRequest +=            ”
    xmlRequest +=        ”
    xmlRequest +=    ”
    xmlRequest +=    ”
    xmlRequest +=        ‘2’
    xmlRequest +=        ”
    xmlRequest +=            ‘CHICAGO’
    xmlRequest +=            ”
    xmlRequest +=                ‘IL
    xmlRequest +=            ”
    xmlRequest +=            ‘60610’
    xmlRequest +=            ”
    xmlRequest +=                ‘US’
    xmlRequest +=            ”
    xmlRequest +=            ”
    xmlRequest +=        ”
    xmlRequest +=    ”
    xmlRequest +=”
    xmlRequest +=”
    xmlRequest +=    ‘false’
    xmlRequest +=    ” + totalWeight + ”
    xmlRequest +=”

    var xmlRequestFormatted = xmlRequest;
    //var responseFromKDL = nlapiRequestURL(url, xmlRequestFormatted, headers, “POST”);

    return xmlRequestFormatted;
    // returns xml with escaped quotes
    This is a cached copy. Click here to see the original post.

  • #5656

    CREECE

    Have you tried setting the application/content type as JSON?


    nic kcd replied on 12/27/2016, 08:02 AM: It is set as application/json when calling the RESTlet. However the shipping API is accepts XML. But the problem is that the string xmlRequest is being returned like JSON. Or at least I think it is. It is being returned with the escaped quotation marks and then I can’t use nlapiRequestURL with the formatted XML.

  • #5657

    ironside

    Are you sure it returns XML escaped quotes, or is that just how it looks when you nlapiLogExecution() the value?

    Also, I recommend jsonix for handling javascript->XML serialization.

    p.s. have you ever considered lodash to simplify your business logic?

  • #5658

    nic kcd

    @iron

  • #5659

    nic kcd

    ironside I was wondering if it was not being actually being escaped with quotes myself (it is not nlapiLogExecution, it is just what is being returned in the body of Postman). However the reason I think it is being escaped is that I nlapiResolveURL is returning a syntax error when I pass xmlRequest to it. When I take that value, remove the escaped quotes, then pass it to the other API manually, then it is successful.

    I will look into jsonix.

    Also I have been highly considering lodash as I am very interested in functional approach. I am guessing you are referring to this specifically some of the map and reduce in this code. I would love to hear your opinion and see some sample code (doesn’t have to be tested) on how it would make the logic easier to reason about.

    Thanks

  • #5660

    ironside

    I’m not sure where you’re using nlapiResolveURL – maybe you mean the nlapiRequestURL() call? Once idea from left field would be to try calling .toString() on your xmlRequest prior to sending. If you are calling this restlet from postman and seeing escapted XML on the response, that’s separate from the string being sent to nlapiRequestURL.

    Regarding your business logic, one example might be your total weight calculation.

    If I follow your code correctly, with lodash that could be something like:

    Code:
    var totalWeight = _.sumBy(parsed.freight.items, function(object) { return object.weight * object.qty } );
    or even a bit simpler if you use TypeScript

    Code:
    var totalWeight = _.sumBy(parsed.freight.items, o => o.weight * o.qty );
    Using sumBy is both simpler code and arguably more descriptive of the intent.

    Originally posted by nic kcd

    View Post

    ironside I was wondering if it was not being actually being escaped with quotes myself (it is not nlapiLogExecution, it is just what is being returned in the body of Postman). However the reason I think it is being escaped is that I nlapiResolveURL is returning a syntax error when I pass xmlRequest to it. When I take that value, remove the escaped quotes, then pass it to the other API manually, then it is successful.

    I will look into jsonix.

    Also I have been highly considering lodash as I am very interested in functional approach. I am guessing you are referring to this specifically some of the map and reduce in this code. I would love to hear your opinion and see some sample code (doesn’t have to be tested) on how it would make the logic easier to reason about.

    Thanks

  • #5661

    nic kcd

    Well I got it working somehow. Not really sure what the issue was, but at this point I just want to move on with my life lol.

    Thanks for your help and also thanks for the encouragement in using lodash as well ironside

    Do you use TypeScript in all/most of your SuiteScripts? I would like to be able to use ES6 (I know TypeScript offers more than just compiling ES6) but I feel compiling the script would be pretty time consuming. Do you use npm nsmockup or something like that to test/debug?

  • #5662

    ironside

    Originally posted by nic kcd

    View Post

    I feel compiling the script would be pretty time consuming. Do you use npm nsmockup or something like that to test/debug?

    I use Webstorm, which is compiling the code behind the scenes all the time. Every time I change something in a .TS file the ES5 JS file for NetSuite is ready within a couple seconds. Compile time isn’t something I even notice because it takes me a second or two to be ready to upload a new version of the file anyway

You must be logged in to reply to this topic.