Skip to content

Instantly share code, notes, and snippets.

@zenoyu
Created October 10, 2019 23:27
Show Gist options
  • Select an option

  • Save zenoyu/a30df1e88859214b29d900a490c73692 to your computer and use it in GitHub Desktop.

Select an option

Save zenoyu/a30df1e88859214b29d900a490c73692 to your computer and use it in GitHub Desktop.
SFCC HTTP Service: Making external REST API Call
/**
* Your 1st Hello worldService
*
*/
var LocalServiceRegistry = require('dw/svc/LocalServiceRegistry');
/**
* Helloworld
* @returns {Object} Helloworld Service
*/
function getHelloworldService() {
var helloworldService = LocalServiceRegistry.createService('test.http.helloworld', {
createRequest: function (svc, args) {
// authorization
var user = svc.getConfiguration().getCredential().getUser();
var password = svc.getConfiguration().getCredential().getPassword();
svc.addHeader('Authorization', 'Bearer user="' + user + '",password="' + password + '"');
svc.addHeader('Content-Type', 'application/json');
if (svc.getRequestMethod() === 'POST') {
return JSON.stringify(args);
}
return svc;
},
parseResponse: function (svc, output) {
return output;
},
getRequestLogMessage: function (reqObj) {
return reqObj;
}
});
return helloworldService;
}
// export
module.exports = {
/**
* Hello World
* @returns {Object} Helloworld Service
*/
initHelloworldService: function () {
var helloworldService = getHelloworldService();
helloworldService.setRequestMethod('POST');
helloworldService.setURL(helloworldService.getURL() + '/your-api-endpoint/hello-world');
return helloworldService;
}
};
@mitashki12

Copy link
Copy Markdown

And to pass params to the POST request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment