Open In App

AngularJS $httpParamSerializer Service

Last Updated : 08 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)
                $http.post('https://example.com/api', serializedData, {
                    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
                $http.post("http://example.com", data, config)
                    .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:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads