0
votes

I am currently using angular 4 to build my web application.In that i am getting the data via rest calls to webservices,when i post data i want to know if i can change the body presentation (second param in post method) with an object so the instruction will be easier.

 updatePasswordWithObservable(userToAdd:User) {
        let headers = new Headers({ 'Content-Type': 'application/json' });
            let options = new RequestOptions({ headers: headers });


                       this.http.post(this.updatePwdUrl, 

{
                            "internalId": 0,
                            "oldPwd": "",
                            "pwd": "123",
                            "reset": true,
                            "userEntry": {
                              "activeNotifications": true,
                              "aspects": [
                                {
                                  "aspectName": "string",
                                  "internalId": 0
                                }
                              ],
                              "email": "[email protected]",
                              "enabled": true,
                              "lastName": "waaaa",
                              "login": "[email protected]",
                              "name": "souad12219",
                              "phone": ""
                            }
                          }

) .subscribe( res => { console.log(res); }, err => { console.log("Error occured"); } ); }

2

2 Answers

1
votes

Yes, you can use an object. Though, you will need to convert it to JSON first. You can use JSON.stringify to do so:

updatePasswordWithObservable(userToAdd:User) {
    let data = JSON.stringify({
                            "internalId": 0,
                            "oldPwd": "",
                            "pwd": "123",
                            "reset": true,
                            "userEntry": {
                              "activeNotifications": true,
                              "aspects": [
                                {
                                  "aspectName": "string",
                                  "internalId": 0
                                }
                              ],
                              "email": "[email protected]",
                              "enabled": true,
                              "lastName": "waaaa",
                              "login": "[email protected]",
                              "name": "souad12219",
                              "phone": ""
                            }
                          });

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    this.http.post(this.updatePwdUrl, data)
        .subscribe( res => { console.log(res); }, err => { console.log("Error occured"); } ); 
}
0
votes

You can POST data in Json format like as:

public loadApp(): Observable<any> {

    const data = {
      'type' : 'ReadRequest',
      'query' : 'get',
      'parameters' : { 'app' : 'new' }
    };

    return this.http.post(null, data)
      .map(res => (<any>res)._body === '' ? {} : res.json())
      .catch(this.handleError);
  }

  private handleErrorObservable(error: Response | any) {
    return Observable.throw(error.message || error);
  }

  private handleError(error: any): Promise<any> {
    console.error('loadAuditories: ', error);
    return Promise.reject(error.message || 'Server error: ' + error);
  }

Pay attention on the const data. It is simplest object JS.