'how to create web service Api in Angular 8?
can anyone help me how to create a web service API in angular 8. I am in new in angular 8. I am currently working on angular 8 project. Please Help..
from POSTMAN => api
Solution 1:[1]
Here I have provided a sample code for creating the web service API in Angular for your reference.
Service.ts
import { HttpClient, HttpHeaders, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Injectable({
providedIn: 'root'
})
export class UserService {
constructor(private fb: FormBuilder,private http: HttpClient) { }
readonly BaseURI = 'http://localhost:53189';
}
register() {
return this.http.post(this.BaseURI + '/auth/Register', body);
}
login(formData: any) {
return this.http.post(this.BaseURI + '/auth/Login', formData);
}
getData(){
return this.http.get(this.BaseURI +'/api/categories');
}
viewCategory(id: any) {
return this.http.get(this.BaseURI + '/api/categories/'+id);
}
postData(formData: any){
return this.http.post(this.BaseURI +'/api/categories',formData);
}
putData(id: any,formData: any){
return this.http.put(this.BaseURI +'/api/categories/'+id,formData);
}
deleteData(id:any){
return this.http.delete(this.BaseURI +'/api/categories/'+ id);
}
getProduct() {
return this.http.get(this.BaseURI +'/api/products')
}
viewProducts(id: any) {
return this.http.get(this.BaseURI + '/api/products/'+id);
}
postProduct(formData: any){
return this.http.post(this.BaseURI +'/api/products',formData);
}
putProduct(id: any,formData: any){
return this.http.put(this.BaseURI +'/api/products/'+id,formData);
}
deleteProduct(id:any)
{
return this.http.delete(this.BaseURI +'/api/products/'+ id);
}
getCart(userId:any) {
return this.http.get(this.BaseURI +'/api/cart/GetId/'+ userId);
}
addProductToCart(userId:any,productId:any) {
return this.http.put(this.BaseURI +'/api/cart/add/'+userId+'/'+productId,null);
}
decreaseQuantity(userId:any,id:any) {
return this.http.put(this.BaseURI +'/api/cart/decreaseQuantity/'+ userId+'/' + id,null);
}
Productbycategory(categoryId: any) {
return this.http.get(this.BaseURI +'/api/products/Getproduct/' + categoryId );
}
getCategory() {
return this.http.get(this.BaseURI +'/api/categories');
}
// getUserProfile() {
// var tokenheader= new HttpHeaders({ 'Authorization':'Bearer '+ localStorage.getItem('token')});
// return this.http.get(this.BaseURI + '/UserProfile',{headers:tokenheader});
// }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pooja |
