URL FOR PUT request is http://localhost:8080/user/userId/update
user is a body with JSON data and userId is string.Update is a method.
where user={ "name":"value", "age":"value:, "gender":"value" }
userId=123456
How to make angular httpClient PUT request in angular 4 and above
You should use HttpClient to do that from a service class.
Create a service and Inject HttpClient to your service.
and then in the putRequestHttp method where you are making http call:
putRequestHttp(userObj): any {
//optional url query params for request
const params = new HttpParams()
.set('param1', param1Value)
.set('param2', param2Value);
return this.httpClient.put(`my-url-http`, userObj, { params } )
}
You need to subscribe to putRequestHttp method of service from your component. pass the user object to it and your request will go fine.
Check the official tutorial guide here.
Same as above but adding a little more typing / different syntactical option. I just find this easier to read / cleaner. Assuming User is a defined type and the return type of the route is the updated user also:
putUser = (user: User): Observable<User> => {
return this.httpClient.put<User>('api route', user);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With