Created
October 10, 2019 23:27
-
-
Save zenoyu/a30df1e88859214b29d900a490c73692 to your computer and use it in GitHub Desktop.
SFCC HTTP Service: Making external REST API Call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And to pass params to the POST request?