I've been attempting to convert JSON data into formURL encoded data, but I'm encountering issues.
Here's my HTTP post code snippet:
$http.post(API_ENDPOINT.login, credentials, {
transformRequest: transformRequestAsFormPost
})
This is my transform request function:
'use strict';
define(['app-module'], function(app) {
$app.info('transformRequest initialized');
return app.factory('transformRequestAsFormPost', function() {
function transformRequest(data, getHeaders) {
var headers = getHeaders();
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8";
return (serializeData(data));
}
function serializeData(data) {
if (!angular.isObject(data)) {
return ((data === null) ? "" : data.toString());
}
var buffer = [];
for (var name in data) {
if (!data.hasOwnProperty(name)) {
continue;
}
var value = data[name];
buffer.push(
encodeURIComponent(name) +
"=" +
encodeURIComponent((value == null) ? "" : value)
);
console.log(buffer)
}
var source = buffer.join("&").replace(/%40/g, "@");
return (source);
}
return (transformRequest);
});
});
I am struggling to identify the issue. Whenever I pass a JSON object, it returns a string.