Search code examples
angularrequesthttp-headershttp-patch

How can I make Patch request in Angular 12?


How can I send JSON data with patch request and which method to connect to API?

private baseUrlRemoveNotifications: string =
   "api/v1.0/UploadDownload/removeNotifications";  
public removeNotificationForUser(onlineUserModel: OnlineUserModel) {
   let username = onlineUserModel.userName;
   const url = `${this.baseUrlRemoveNotifications}`;
   const options = {
     headers: new HttpHeaders({
       "Content-Type": "application/json",
     }),
   };
   return this.http.patch(
     `${this.baseUrlRemoveNotifications}`,
     username,
     options
   );
 }

Solution

  • You can try with this slice of code :

    public removeNotificationForUser(onlineUserModel: OnlineUserModel) {
        let targetUsername = onlineUserModel.userName;
        const url =
          this.baseUrlRemoveNotifications +
          "/" +
          this.cookieService.get("username") +
          "/" +
          targetUsername;
        const body = {};
        const options = {
          headers: new HttpHeaders({
            "Content-Type": "application/json",
            "X-XSRF-TOKEN": this.cookieService.get("XSRF-TOKEN"),
          }),
        };
        this.http.patch<any>(url, body, options).subscribe((data) => {
          console.log("hi this is put request", data);
        });
      }