Submitting a file to a third party service in SFCC

Salesforce B2C Commerce Cloud provides developers and architects with a framework to integrate third-party services, making it much more streamlined to get up and running.

With the LocalServiceRegistry, you get some neat features like configuration management from the business manager, logging, and service monitoring!

There are examples available on how to use this system, but how do you send a file to a service using it? There isn’t an example available for this (except for an FTP service that, by definition, works with files).

TLDR; solution

Multipart/Form-Data

A separate article is available if your service uses the Multipart format.

Single File

For those who want a code snippet to copy and paste, here you go!

				
					LocalServiceRegistry.createService('test.http.post', {
        createRequest: function (svc, args) {
            var File = require('dw/io/File');
            // In case you have a variable endpoint
            svc.setURL(args.uploadUrl);

            return new File(args.filePath);
        },
        parseResponse: function(svc, client) {
            return client.text;
        }
    })
				
			

Or if you want more control over your request:

				
					LocalServiceRegistry.createService('test.http.post', {
	 	createRequest: function (svc, args) {
	 	    // In case you have a variable endpoint
			svc.setURL(args.uploadUrl);
			return args;
    	},
    	executeOverride: true,
        execute: function(svc, args) {
            var File = require('dw/io/File');
            var client = svc.client;
     
            client.send(new File(args.filePath));
        },
        parseResponse: function(svc, client) {
            return client.text;
        }
});
				
			

Configuration in the Business Manager

In the Business Manager go to:

“Administration > Operations > Services”

And click the “New” button on the bottom right. Here we can configure our service:

This example service is configured with the following values:

  • Name: It can be any name as long as it matches the name (ID) used in the code.

  • Type: In this case, HTTP service is selected. This ensures the Service Framework behind the scenes uses the correct client (HTTP).

  • Service Mode: To call the service endpoint, the value needs to be “Live.”

  • Log Name Prefix: If you want to debug and have all request and response data in a dedicated file for this service, fill this in.

  • Communication Log Enabled: This must be enabled to debug the requests and responses through logging.

  • Force PRD Behavior in Non-PRD Environments: If you filter the logs and want to test as if it is a production environment, enable this.

  • Profile: Select the profile to be used (timeouts, rate limiting, circuit breaker.)

  • Credentials: Depending on the service, this needs to be configured.

Now everything is configured in the Business Manager, and we can move on to writing some code.

Returning the file in createRequest

				
					LocalServiceRegistry.createService('test.http.post', {
        createRequest: function (svc, args) {
            ...

            return new File(args.filePath);
        }
    })
				
			

No rocket science is happening here. The Service Framework automatically detects it is a file being returned and executes the appropriate logic on the HTTPClient “beneath the surface”. 

In case we override the execute logic

				
					LocalServiceRegistry.createService('test.http.post', {
	 	createRequest: function (svc, args) {
	 	    ...
    	},
    	executeOverride: true,
        execute: function(svc, args) {
            var File = require('dw/io/File');
            var client = svc.client;
     
            client.send(new File(args.filePath));
        },
        parseResponse: function(svc, client) {
            ...
        }
});
				
			

Overriding the “execute” logic is quite simple. As explained in the ServiceCallback documentation, we can use the “executeOverride” flag to write some custom code on how the external service is called.

In this case, we get the original HTTPClient from the Service using “svc.client.

This client has a function for us to send a file over to an external endpoint: send(File)

Effects on logging

When working with files, the function “filterLogMessage” receives what you return in the “createRequest” call rather than the request’s body.

This might be a good thing if you are sending large files.

				
					INFO PipelineCallServlet|1954691750|Sites-RefArch-Site|Login-Post|PipelineCall|kxZB-_nwKL custom.service.test.http.post.COMM []  Request:
[File path=/TEMP/my-file.zip]
				
			
Finger touching loading file button on a touch screen

Table of Contents

Facebook
Twitter
LinkedIn