Skip to content

Instantly share code, notes, and snippets.

@eloone
Created July 29, 2015 12:50
Show Gist options
  • Save eloone/4b528e4b41ddd32a401d to your computer and use it in GitHub Desktop.
Save eloone/4b528e4b41ddd32a401d to your computer and use it in GitHub Desktop.
(function(angular){
angular.module('app.lib.http.interceptorRequestFormatter', ['nvision.lib.xmlRequestFormatter'])
.config(['$httpProvider',function($httpProvider) {
$httpProvider.interceptors.push('InterceptorRequestFormatter');
}])
.factory('InterceptorRequestFormatter', ['XmlRequestFormatter', InterceptorRequestFormatterProvider]);
function InterceptorRequestFormatterProvider(XmlRequestFormatter){
// Formats the request data
// @reqData is a json string
// @return is a json string
function formatDataToXml(reqData){
var endJson = XmlRequestFormatter.reqDataToXml(JSON.parse(reqData));
return JSON.stringify(endJson);
}
return {
request: function(config) {
var allowedWriteMethods = ['POST', 'PATCH', 'DELETE'];
if(_.contains(allowedWriteMethods, config.method) &&
!_.some(config.transformRequest, function(reqHandler){
return reqHandler === formatDataToXml;
})
) {
//try to use unshift to not have to do stringify
config.transformRequest.push(formatDataToXml);
}
return config;
}
};
}
}(window.angular));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment