Umesh
Umesh

Reputation: 55

Angular Put request with body example

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

Upvotes: 3

Views: 19037

Answers (2)

Ryan
Ryan

Reputation: 177

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);
}

Upvotes: 1

nircraft
nircraft

Reputation: 8478

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.

Upvotes: 6

Related Questions