AngularJS HTTP Client – Communicate with APIs Using $http (2025 Guide)
Introduction – Why Learn AngularJS HTTP Client?
Modern web applications are driven by data—whether it’s pulling content from a server or submitting a form. AngularJS offers a built-in $http service that allows seamless communication with RESTful APIs using methods like GET, POST, PUT, and DELETE. This enables your AngularJS app to dynamically fetch, send, and manipulate data from remote sources.
In this guide, you’ll learn:
- What
$httpis and how it works in AngularJS - Syntax for HTTP methods (GET, POST, etc.)
- Handling responses and errors
- Best practices for using
$httpwith services and controllers
What is AngularJS $http?
$http is a core AngularJS service that allows your application to make HTTP requests to a backend server or REST API.
Key Features:
- Built-in support for asynchronous HTTP calls
- Returns promises (then/catch pattern)
- Supports interceptors for global request/response processing
Common $http Methods
| Method | Description |
|---|---|
$http.get() | Fetch data from server |
$http.post() | Send new data to server |
$http.put() | Update existing data |
$http.delete() | Remove data from server |
Example – GET Request to Fetch Data
app.controller("UserCtrl", function($scope, $http) {
$http.get("https://api.example.com/users")
.then(function(response) {
$scope.users = response.data;
})
.catch(function(error) {
console.error("Error fetching users:", error);
});
});
Fetches a list of users from the API and binds them to the view.
Example – POST Request to Send Form Data
$scope.submitForm = function(user) {
$http.post("https://api.example.com/users", user)
.then(function(response) {
$scope.message = "User created successfully!";
})
.catch(function(error) {
$scope.error = "Submission failed!";
});
};
Sends new user data to the server when the form is submitted.
Example – PUT Request to Update Resource
$scope.updateUser = function(user) {
$http.put("https://api.example.com/users/" + user.id, user)
.then(function(response) {
$scope.message = "User updated!";
});
};
Example – DELETE Request
$scope.deleteUser = function(userId) {
$http.delete("https://api.example.com/users/" + userId)
.then(function(response) {
$scope.message = "User deleted!";
});
};
Handling Response and Errors
Every $http call returns a promise with .then() and .catch():
$http.get(url)
.then(function(response) {
// success logic
})
.catch(function(error) {
// error handling
});
You can also use .finally() if needed.
HTTP Configuration Options
Each $http method accepts a config object:
$http({
method: 'POST',
url: '/api/products',
data: { name: "Book", price: 99 },
headers: { 'Content-Type': 'application/json' }
});
Adding Custom Headers
$http.get("/api/secure-data", {
headers: {
'Authorization': 'Bearer ' + token
}
});
Use this for authenticated requests.
Creating a Reusable Service for HTTP Calls
app.service("UserService", function($http) {
this.getAll = function() {
return $http.get("/api/users");
};
this.create = function(user) {
return $http.post("/api/users", user);
};
});
Use in controller:
UserService.getAll().then(function(res) {
$scope.users = res.data;
});
Keeps your HTTP logic clean and modular.
Real-World Use Cases
- Contact forms that send data to backend
- Live dashboards that pull reports or analytics
- CRUD applications (Create, Read, Update, Delete)
- eCommerce product listings from APIs
- Admin panels managing user roles
Best Practices for $http
✔️ Use services to organize HTTP logic
✔️ Handle errors gracefully with .catch()
✔️ Avoid hardcoded URLs—use constants/config
✔️ Set default headers globally using $httpProvider
✔️ Consider interceptors for auth and logging
Summary – Recap & Next Steps
AngularJS’s $http service is a powerful tool for integrating external APIs and backends into your web application. It provides full support for modern RESTful HTTP methods, response handling, and modular code organization.
Key Takeaways:
- Use
$httpfor GET, POST, PUT, DELETE requests - Handle async responses with
.then()and.catch() - Structure calls into services for reusability
- Add headers for authenticated or JSON API requests
Real-world Relevance:
Used in dashboards, CMS systems, chat apps, admin panels, and any application needing backend connectivity.
FAQ – AngularJS HTTP Client
What is $http in AngularJS?
$http is a service used to make AJAX calls (GET, POST, PUT, DELETE) to external APIs or backend servers.
Is $http promise-based?
Yes. It returns promises that you can handle with .then(), .catch(), and .finally().
How do I send headers in a $http request?
Use the headers property inside the $http config object.
Can I create custom services using $http?
Absolutely. You can create services that encapsulate your API logic and reuse them across controllers.
Share Now :
