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
------------------------------
Original Message:
Sent: Wed February 22, 2023 11:13 PM
From: Jyoti Yadav
Subject: Please suggest How to remove new line ('\n') from text using gatewayscript
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
Original Message:
Sent: Wed February 22, 2023 05:30 PM
From: Steve Linn
Subject: Please suggest How to remove new line ('\n') from text using gatewayscript
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
Original Message:
Sent: Wed February 22, 2023 03:48 AM
From: Jyoti Yadav
Subject: Please suggest How to remove new line ('\n') from text using gatewayscript
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
------------------------------