-
Notifications
You must be signed in to change notification settings - Fork 0
multipart
Dmytro Katashev edited this page Aug 12, 2024
·
1 revision
The multipart utility provides functions for handling multipart form data in Salesforce Commerce Cloud (SFCC). This utility is essential for parsing and formatting multipart bodies, particularly when working with file uploads or complex form submissions in web services.
Description: Parses a raw multipart body into an array of parts, where each part includes headers and the associated body.
-
Parameters:
-
boundary(string): The boundary string that separates the parts in the multipart body. -
body(dw.util.Bytes): The raw multipart body as a byte stream.
-
-
Returns:
-
Array.<MultipartChunk>: The parsed parts, each containing headers and the corresponding body.
-
Usage Example:
var Bytes = require('dw/util/Bytes');
var multipart = require('*/cartridge/scripts/util/multipart');
var boundary = '--BoundaryString';
var body = new Bytes('--BoundaryString\r\nContent-Disposition: form-data; name="file"; filename="example.txt"\r\n\r\nFile content\r\n--BoundaryString--');
var parts = multipart.parse(boundary, body);
console.log(parts);
// [
// {
// headers: { 'content-disposition': 'form-data; name="file"; filename="example.txt"' },
// body: <Bytes containing "File content">
// }
// ]Description: Converts an array of parts into a formatted multipart body string.
-
Parameters:
-
boundary(string): The boundary string to use between parts. -
parts(Array.): An array of parts to include in the multipart body, where each part contains headers and the corresponding body.
-
-
Returns:
-
string: The formatted multipart body as a string.
-
Usage Example:
var multipart = require('*/cartridge/scripts/util/multipart');
var boundary = '--BoundaryString';
var parts = [
{
headers: { 'content-disposition': 'form-data; name="file"; filename="example.txt"' },
body: 'File content'
}
];
var multipartBody = multipart.format(boundary, parts);
console.log(multipartBody);
// --BoundaryString\r\ncontent-disposition: form-data; name="file"; filename="example.txt"\r\n\r\nFile content\r\n--BoundaryString--Represents a chunk of multipart data, consisting of HTTP headers and the corresponding body.
-
Type:
Object-
headers(Object.<string, string>): The HTTP headers associated with this part. -
body(dw.util.Bytes): The body of this part as a byte stream.
-
Usage Example:
var chunk = {
headers: {
'content-disposition': 'form-data; name="file"; filename="example.txt"'
},
body: new dw.util.Bytes('File content')
};