Original Message:
Sent: Sat June 03, 2023 09:53 AM
From: mayur gharat
Subject: Send multipart/form-data As image File to Backend
Hello Steve ,
We are using v5 compatible gateway .
I have tried with other code also with apim function but still error is same .(500
errorMessage": "internal error",
)
Data looks ok with this script also .
Data from GS which is not working
Data from Pass through which is working
sample2.js
var hm = require('header-metadata');
apim.readInputAsBuffer(function (error, buffer) {
if (error)
{
// handle error
session.output.write (error.errorMessage);
} else {
var base64Image = "base64 encoded data "
var decoded = new Buffer(base64Image,'base64')
var prefix = `----------------------------921934391642645198236509
Content-Disposition: form-data; name="wsq_file"; filename="test.wsq"
Content-Type: application/octet-stream
`;
var suffix = `
----------------------------921934391642645198236509--
`;
var buf1 = Buffer.from(prefix);
var buf2 = Buffer.from(decoded);
var buf3 = Buffer.from(suffix);
var arr1 = [buf1, buf2, buf3];
var newBuff = Buffer.concat(arr1);
var contentType = 'multipart/form-data; boundary=' + '--------------------------921934391642645198236509';
apim.setvariable('message.headers.Content-Type', contentType);
// hm.current.set('Content-Type', contentType);
session.output.write(newBuff);
console.error(newBuff);
apim.output(contentType);
}
});
Regards ,
Mayur
------------------------------
mayur gharat
Original Message:
Sent: Fri June 02, 2023 08:49 AM
From: Steve Linn
Subject: Send multipart/form-data As image File to Backend
Hi Mayur,
Do you have any indication from the backend what it does not like about your request? Also, since this is the API Connect forum, are you using a v5 compatible gateway or an API Gateway? Or since your code is not using any apim or context functions as I would expect to see in an API GatewayScript policy, but instead is using the header-metadata module to directly set the content-type header, is this GatewayScript being executed within an action of a Multi-Protocol Gateway Processing policy rule to simply test out the GatewayScript? Structurally the multipart form looks ok with the boundary of the content type header matching. The only thing I can think of is the image itself and the base64 decoding in the new Buffer statement. When you say the data "seems ok" how are you looking at it? Perhaps you could use as a test the fs module to write that Buffer to a temporary file and view the contents?
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Thu June 01, 2023 06:43 PM
From: mayur gharat
Subject: Send multipart/form-data As image File to Backend
Hi Steve Linn ,
Thanks for your reply .
I have done some changes at code level as suggested now the data seems ok but still from
backend its failing .
Can you check the attached code is anything missing ?
Regards ,
Mayur
------------------------------
mayur gharat
Original Message:
Sent: Thu June 01, 2023 11:05 AM
From: Steve Linn
Subject: Send multipart/form-data As image File to Backend
Hi Mayur,
A couple things:
First, your code is doing
base64Image.toString()
so there will be utf-8 encoding done by the toString that is changing your binary data. Just the act of concatenation is probably doing a toString function under the covers and doing the same encoding. When dealing with a multipart form where you have binary data it is best to build it by creating an array of buffers with the different parts of the overall form, and you can use string concatenation for the ascii portions of that as you're doing, but have your image as a buffer that you simply push to the array, and once you have the entire form in the array, then concatenate the buffers into a new buffer, for example
var newBuffer = Buffer.concat(newBufferArray);
Then you would output your newBuffer with the session.output.write function.
Second, what gateway type are you using? Your code is doing a
session.input.readAsBuffer(function (error, buffer) {
but other than checking the variable error
you never use the variable buffer
, so not sure what you're trying to do with this. What data are you attempting to readAsBuffer? If you're using a v5 compatible gateway, the input context is the particular input context to the GatewayScript DataPower action within the v5c framework which may not be what you think it is and will most probably give you an empty buffer, and I'm not sure what it would give you if you have an api gateway.
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Mon May 29, 2023 03:45 PM
From: mayur gharat
Subject: Send multipart/form-data As image File to Backend
HI , can anyone help me on this .
Also i have attached a part of data which is working with pass through service and which is not working with GS .
Non -Working.
Working
------------------------------
mayur gharat
Original Message:
Sent: Sat May 27, 2023 05:56 AM
From: mayur gharat
Subject: Send multipart/form-data As image File to Backend
Hi Team,
I have an requirement to send the base64 encoded image content to backend as form-Data content.
I have GS , which converting the data and sending to backend but we are getting error from backend as inavalid image also
as i have observe that in content , having some junk data in GS output .
I have GS file attached ,Can you please help me on this.
sample2.js
var hm = require('header-metadata');
apim.readInputAsBuffer(function (error, buffer) {
if (error)
{
// handle error
session.output.write (error.errorMessage);
} else {
var base64Image = "base64 encode data "
var decoded = new Buffer(base64Image,'base64')
var prefix = `----------------------------921934391642645198236509
Content-Disposition: form-data; name="wsq_file"; filename="test.wsq"
Content-Type: application/octet-stream
`;
var suffix = `
----------------------------921934391642645198236509--
`;
var buf1 = Buffer.from(prefix);
var buf2 = Buffer.from(decoded);
var buf3 = Buffer.from(suffix);
var arr1 = [buf1, buf2, buf3];
var newBuff = Buffer.concat(arr1);
var contentType = 'multipart/form-data; boundary=' + '--------------------------921934391642645198236509';
apim.setvariable('message.headers.Content-Type', contentType);
// hm.current.set('Content-Type', contentType);
session.output.write(newBuff);
console.error(newBuff);
apim.output(contentType);
}
});
------------------------------
mayur gharat
------------------------------