RRB
RRB

Reputation: 2116

Angular post request parameters not being passed correctly

I am trying to do a post request in Angular. I need to pass a model as a parameter however I am getting an error.

Here is my code

Model

  protected initModel() {
    this.model = {
      basics: null,
      retirements: null,
      risk: null,
      users: null
    };
  }

Function to assign values to model

  saveTemp( args: { tabName: string, tabModel: any } ) {
    switch (args.tabName) {
      case 'basics': {
        this.model.basics = args.tabModel;
        // console.log(this.model.basics);
        break;
      }
      case 'retirement': {
        this.model.retirements = args.tabModel;
        break;
      }
      case 'risk': {
        this.model.risk = args.tabModel;
        break;
      }
      case 'users': {
        this.model.users = args.tabModel;
        break;
      }
      default: {
        break;
      }
    }
  }

Post request

const token = localStorage.getItem('token');
const headers = new HttpHeaders()
  .set('Authorization', `Bearer ${token}`)
  .set('Content-Type', 'application/json');
const options = { headers: headers };
this.http.post('https://localhost:44345/api/Corporates/Create', this.saveTemp( args: { basic, this.model.basics } ), options)
.subscribe(data => {
  console.log(data);
})

The error is me passing the saveTemp function with parameter is the post request "Expected 1 arguments, but got 2."

enter image description here

Upvotes: 0

Views: 52

Answers (2)

Thivanka Saranatha
Thivanka Saranatha

Reputation: 362

another approach for sending model

apiUrl:string="";
getLoggedInUser(model:any): Observable<any> {
  const headers = new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': 'Bearer token'
  })
  return this.http.post<any>(this.apiUrl,model,{headers:headers})
}

Upvotes: 1

Patryk Uszyński
Patryk Uszyński

Reputation: 1119

This line is invalid. this.saveTemp is not invoked correctly and it doesn't return any value.

this.http.post(URL, this.saveTemp( args: { basic, this.model.basics } ), options)

Try to invoke saveTemp before making the request and pass only model:

this.http.post(URL, this.model, options)

Upvotes: 1

Related Questions