AngularJS $httpParamSerializer Service
Last Updated :
24 Apr, 2025
The AngularJS $httpParamSerializer service is a service that is used to serialize data into a URL-encoded string. It is commonly used with the AngularJS $http service to encode request parameters when making HTTP requests.
Syntax:
app.controller('myCtrl', function($scope, $http, $httpParamSerializer) {
// Serialize an object into a URL-encoded string
var serializedData = $httpParamSerializer({
foo: 'bar',
baz: 42
});
});
Parameter: The AngularJS $httpParamSerializer function takes a single object as its parameter.
Return value: It returns a URL-encoded string representation of the object.
Here is an example of using the $httpParamSerializer function:
app.controller('myCtrl', function($scope, $http, $httpParamSerializer) {
// Serialize an object into a URL-encoded string
var serializedData = $httpParamSerializer({
foo: 'bar',
baz: 42
});
});
In this example, the $httpParamSerializer function is called with an object containing two properties: foo and baz. The function returns a URL-encoded string representation of the object, which might look something like this:
foo=bar&baz=42
Here is an example of using the $httpParamSerializer service to serialize data for an HTTP post request:
app.controller('myCtrl', function($scope, $http, $httpParamSerializer) {
var data = {
foo: 'bar',
baz: 42
};
// Serialize the data into a URL-encoded string
var serializedData = $httpParamSerializer(data);
// Make the HTTP request
$http.post('https://example.com/api', serializedData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
// Success
}, function(response) {
// Error
});
});
Here is an example of using the AngularJS $httpParamSerializer service to make an HTTP GET request with URL-encoded request parameters:
app.controller('myCtrl', function($scope, $http, $httpParamSerializer) {
// Serialize the form data into a URL-encoded string
var serializedData = $httpParamSerializer($scope.formData);
// Make the HTTP request
$http.get('https://example.com/api', {
params: serializedData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
// Success
}, function(response) {
// Error
});
});
Example 1: In this example, we have an AngularJS app with a single controller called mainCtrl. The mainCtrl controller has a scope function called submitForm which is called when the form is submitted. The submitForm function uses the $httpParamSerializer service to serialize the form data into a URL-encoded string and then uses the AngularJS $http service to make an HTTP POST request with the serialized data as the request body. The Content-Type header is set to application/x-www-form-urlencoded to indicate that the request body is URL-encoded.
HTML
<!doctype html>
< html ng-app = "myApp" >
< head >
< script src =
</ script >
< script >
var app = angular.module('myApp', []);
app.controller('mainCtrl',
function ($scope, $http, $httpParamSerializer) {
$scope.submitForm = function () {
// Serialize the form data into a URL-encoded string
var serializedData = $httpParamSerializer($scope.formData);
// Make the HTTP request(Here, we
// are using the Demo HTTP request)
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function (response) {
// Success
}, function (response) {
// Error
});
};
});
</ script >
</ head >
< body ng-controller = "mainCtrl" >
< form ng-submit = "submitForm()"
ng-model = "formData" >
< label >Name:</ label >
< input type = "text"
ng-model = "formData.name" required>
< br >
< label >Email:</ label >
< input type = "email"
ng-model = "formData.email" required>
< br >
< button type = "submit" >
Submit
</ button >
</ form >
</ body >
</ html >
|
Output:
Example 2: Here is an example of using the AngularJS $http service to send an HTTP POST request with an XML payload. Here, we define an AngularJS app and controller that uses the $http service to send an HTTP POST request with an XML payload. We create a scope variable called xmlData that holds the XML data. The XML data is sent in the request as the body data, and the ‘Content-Type’ header is set to ‘application/xml’ to indicate that the request contains XML data. The response is handled by the then method of the $http.post request, which logs the response in the browser’s console.
HTML
<!DOCTYPE html>
< html ng-app = "myApp" >
< head >
< style >
h1 {
color: green
}
button {
color: white;
background-color: black;
height: 30px;
width: 130px;
padding: 3px;
margin: 5px;
border-radius: 5px;
}
textarea {
width: 500px;
height: 300px;
padding: 5px 15px;
margin: 5px 0;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
display: block
}
</ style >
< script src =
</ script >
< script >
var app = angular.module("myApp", []);
app.controller("MyController", function ($scope, $http) {
$scope.xmlData =
"< root >< element1 >value1</ element1 >< element2 >value2</ element2 ></ root >";
$scope.submit = function () {
var config = {
headers: {
'Content-Type': 'application/xml'
}
};
var data = $scope.xmlData;
// Demo Http Request
.then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
}
});
</ script >
</ head >
< body ng-controller = "MyController" >
< center >
< h1 > Geeksforgeeks</ h1 >
< h3 >
AngularJS $httpParamSerializer service
</ h3 >
< form >
< textarea ng-model = "xmlData" ></ textarea >
< button ng-click = "submit()" >
Submit
</ button >
</ form >
</ center >
</ body >
</ html >
|
Output:

Similar Reads
AngularJS $parse Service
The $parse service in AngularJS is a function that takes an expression string and returns a function that can be used to parse and evaluate the expression. The expression string can contain variables, operators, and function calls. To use the $parse service, you first need to inject it into your Ang
3 min read
AngularJS $interval Service
The $interval service in AngularJS is a function that allows you to execute a function repeatedly at a specified interval. It is similar to the setInterval function in JavaScript, but it is designed to work seamlessly with the AngularJS framework. To use the $interval service, it is first needed to
4 min read
AngularJS $controller Service
AngularJS applications depend on a controller to control the flow of data through an AngularJS application. AngularJS architecture uses the MVC model i.e the Model View Controller. The model is responsible for maintaining the application data, the View for displaying data or some part of data to the
5 min read
AngularJS $locale service
The AngularJS $locale service is a built-in service that provides localization information for the current browser environment. It is designed to help developers create applications that can be easily adapted to different locales and languages. The $locale service exposes a number of properties that
4 min read
AngularJS $exceptionHandler Service
In AngularJS, a service is a function or object that is available for dependency injection (DI) in an AngularJS app. Services are typically used to encapsulate and reuse business logic and other app functionality that is not directly related to the presentation of data in the app. The $exceptionHand
4 min read
AngularJS $rootElement service
The AngularJS $rootElement service is a global service that provides a reference to the root element of the AngularJS application. This can be useful in a variety of scenarios, such as modifying the root element's class name or attributes, or traversing the root element's descendants. Syntax: The sy
3 min read
Angular.js $log Service
The $log service in Angular.js is simply used for logging purposes on the browser console. It is used for the debugging and troubleshooting of the error in the code. It has various implementations like a log, warn, info, error, and debugging, and all the names suggest. It is used for logging, warnin
5 min read
AngularJS $location Service
The $location in AngularJS basically uses a window.location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS. There are various methods in the $location s
4 min read
AngularJS $document Service
In AngularJS, a service is a function or object that is available for dependency injection (DI) in an AngularJS app. Services are typically used to encapsulate and reuse business logic and other app functionality that is not directly related to the presentation of data in the app. The $document serv
3 min read
AngularJS Services
The Services is a function or an object that avails or limit to the application in AngularJS, ie., it is used to create variables/data that can be shared and can be used outside the component in which it is defined. Service facilitates built-in service or can make our own service. The Service can on
4 min read