API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Please suggest How to remove new line ('\n') from text using gatewayscript

    Posted Wed February 22, 2023 03:49 AM

    Hello Team,

    We required to remove new line ('\n') from text using gatewayscript. When we try to search or replace function  with '\n' in gatewayscript, we are getting "Invalid or Unexpected token" error for same.

    Please find below text and script. Kindly help how to remove new line and extra spaces from text using script

    text:

    --------123

    Content-Disposition:form-data; name= "dd"

    eeee

    ------123

    Scirpt:

    var v1= data.serach('"name="dd"\n\n');



    ------------------------------
    Jyoti Yadav
    ------------------------------


  • 2.  RE: Please suggest How to remove new line ('\n') from text using gatewayscript

    Posted Wed February 22, 2023 05:30 PM

    Hi Joyti,
    Please provide the actual code.  Is the serach function a typo that is producing this error?  What is the datatype of the variable data?  You should be able to use a string.replace function, or you could use the string.split('\n') function which will produce an array split on the new line characters.

    Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 3.  RE: Please suggest How to remove new line ('\n') from text using gatewayscript

    Posted Wed February 22, 2023 11:13 PM
    Edited by Jyoti Yadav Wed February 22, 2023 11:14 PM

    Hello Steve,

    Thank you for update.

    Please find below response. I want to fetch "param1" as a Parameter name and "ee" as a Parameter value. To fetch value, i want to remove next (new) line between "" and "".

    I try below code to replace new line but getting "Invalid or unexpected token" error. Please suggest next step.

    Gatewayscript code:

    var bodyAsBlob = apim.getvariable("request.body");
    var bodyAsString = bodyAsBlob.item(0).toBuffer().toString();
    session.output.write(bodyAsString.replace('\n',""));
    apim.output("application/json")

    Response:

    ----------------------------663567524731559544062381
    Content-Disposition: form-data; name="param1"

    ee
    ----------------------------663567524731559544062381

    Thanks and Regards,

    Jyoti



    ------------------------------
    Jyoti Yadav
    ------------------------------



  • 4.  RE: Please suggest How to remove new line ('\n') from text using gatewayscript

    Posted Thu February 23, 2023 12:39 AM

    Hi,

    Can you clear what exactly you require , do you want to have key value pair as output or you just want to remove newLine and spaces from text.

    For removing newLine and spaces, you could use ' inputData.replace(/(\r\n|\n|\r)/gm, " "));'



    ------------------------------
    Jayprakash Yadav
    ------------------------------



  • 5.  RE: Please suggest How to remove new line ('\n') from text using gatewayscript

    Posted Thu February 23, 2023 03:12 AM

    Hello Jayprakash,

    I want "Key" and "Value" pair as a output from "multi-part" request payload. Please suggest how to fetch values. As per IBM suggestion, "multi-part" is not supported for v5c gateway compatible type. So I try to convert response to string and fetch key value data from it.

    I have recevied "missing /" error while using above code to replace new line.



    ------------------------------
    Jyoti Yadav
    ------------------------------



  • 6.  RE: Please suggest How to remove new line ('\n') from text using gatewayscript

    Posted Thu February 23, 2023 03:21 AM

    Can you share your 

    Input Data & Expected Output Data. So it will help you sharing sample snippet 



    ------------------------------
    Jayprakash Yadav
    ------------------------------



  • 7.  RE: Please suggest How to remove new line ('\n') from text using gatewayscript

    Posted Thu February 23, 2023 04:50 AM

    Hello Jayprakash,

    I send request body in "form-data" format and receive response in multi-part form (seperated by boundary).I want to fetech and read it as a key value.

    Please find below response of multipart/form-data as a input and expected output.

    Input:

    ----------------------------088286654678741986667751
    Content-Disposition: form-data; name="kk"

    ggjj
    ----------------------------088286654678741986667751--

    output:

    {"kk":"ggjj"}



    ------------------------------
    Jyoti Yadav
    ------------------------------



  • 8.  RE: Please suggest How to remove new line ('\n') from text using gatewayscript

    Posted Thu February 23, 2023 08:23 AM

    Hi Jyoti,
    First just as a reminder, the code

    bodyAsBlob.item(0).toBuffer().toString()

    the toString() on a multi-part payload will do a utf-8 translation.  Given you're just dealing with form-data, that shouldn't be a problem, but if you should deal with Content-Disposition with a file where the file payload would contain binary non-utf8 data like a .pdf or .jpeg, the toString() will corrupt your payload.  In those cases you should use a Buffer object which supports an indexOf and slice functions to step through and separate the parts in the payload.   As for extracting the name from the Content-Disposition header, I'm assuming you can have many form parts, so I'd get the boundary from the content type header and split the string based upon the boundary string, then with each boundary part, you could use a regex

        let currentPartDisposition = currentPart.slice(currentPart.indexOf('Content-Disposition')).toString();
        var dispositionMatch = currentPartDisposition.match(/Content-Disposition: form-data; name="(.*)"/);
        // if we have a match, the name will be in the 2nd (index 1) array element.  No match, null is returned.  
        // If you have a filename as well you could include that in the regex
        var dispositionMatch = currentPartDisposition.match(/Content-Disposition: form-data; name="(.*)"; filename="(.*)"/);
        // if we have a match, the name will be in the 2nd (index 1) array element, filename  in 3rd (index 2)
        // array element. 

    Then you could do a indexOf to find two consecutive \n characters which should indicate the start of the value and slice that from that location +2 to the end of the part would be the value, actually you'll need to slice off the line feed at the end of the part, in the string.
    Do that for all parts that have a Content-Disposition match, and you should  be able to build an object like

    {"param1": "ee,
     "param2": "ff", 
    ... etc
    }

    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------