Skip to content
the poor coder

angular http PUT

Poor Man
Published
Reading time
3 min read
On this page

Angular HTTP PUT Request#

When working with Angular, there are many HTTP methods that you can use to communicate with a backend API. One of these methods is PUT, which is used to update an existing resource on the server. In this article, we will discuss how to use the Angular HttpClient module to send a PUT request.

How to make a PUT request in Angular?#

To make a PUT request in Angular, you first need to import the HttpClient module from '@angular/common/http'. This module provides a set of powerful APIs to communicate with the server.

TypeScript

import { HttpClient } from '@angular/common/http';

constructor(private http: HttpClient) {}

Once you have imported the HttpClient module, you can use the 'put' method to send a PUT request to the server. The 'put' method takes two arguments: the URL of the resource you want to update and the data you want to send in the request body.

TypeScript

updateResource(url: string, data: any) {
  return this.http.put(url, data);
}

You can also pass additional options as the third argument to the 'put' method. For example, you can set headers or observe the response of the request.

TypeScript

const options = {
  headers: {'Content-Type': 'application/json'},
  observe: 'response'
};

updateResource(url: string, data: any) {
  return this.http.put(url, data, options);
}

Example#

Let's say we have an API that allows us to update a user. We can create a UserService that calls the 'updateResource' method to update the user.

TypeScript

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class UserService {
  private apiUrl = 'https://example.com/api/users/';

  constructor(private http: HttpClient) {}

  updateUser(user: any) {
    const url = this.apiUrl + user.id;
    return this.http.put(url, user);
  }
}

Now we can use the UserService to update a user in our component:

TypeScript

import { Component } from '@angular/core';
import { UserService } from './user.service';

@Component({
  selector: 'app-root',
  template: '
    <form (ngSubmit)="onSubmit()">
      <input [(ngModel)]="user.name" name="name">
      <input [(ngModel)]="user.email" name="email">
      <button type="submit">Update User</button>
    </form>
  '
})
export class AppComponent {
  user = { id: 1, name: 'John Doe', email: 'john@example.com' };

  constructor(private userService: UserService) {}

  onSubmit() {
    this.userService.updateUser(this.user).subscribe(
      response => console.log('User updated successfully.'),
      error => console.log('An error occurred while updating user.')
    );
  }
}

When the user clicks the 'Update User' button, the form data is sent to the server using a PUT request. The server updates the user and returns a response, which is logged to the console.

Conclusion#

In this article, we have learned how to use the Angular HttpClient module to send a PUT request to update a resource on the server. We have also seen how to pass additional options to the 'put' method and how to handle the response of the request. I hope this article has been helpful in understanding how to use the HttpClient module in Angular.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC