Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Put request with body example

Tags:

angular

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

like image 937
Umesh Avatar asked Mar 23 '26 16:03

Umesh


2 Answers

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.

like image 83
nircraft Avatar answered Mar 26 '26 05:03

nircraft


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);
}
like image 20
Ryan Avatar answered Mar 26 '26 07:03

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!