Single value in ionic http post param

Issue

Hi I’m new to ionic/angular and I’m reading someone else code and came across the function below in the Service

 makePostRequest(100)
 
 public makePostRequest(param) {
   return this.http.post('/sample/api', param);
 }

Does this mean param is sent as json body or just a value, online documentation just shows json body as the argument not a single value. Can someone help me with this

Thanks in advance.

Solution

The post body is sent as serialized parameters as explained here.

In the example you have provided, they are calling the makePostRequest() function and providing an argument of 100. When the makePostRequest() function runs, it takes that value from the argument and sets it as a parameter which is then used to populate the body in the POST request.

You can still send single values as you would multiple values with something similar to this:

sendData() {
   const data = {
      number: 100
   };
   return this.http.post('myurl.com', data);
}

Or for multiple:

sendData() {
   const data = {
      number: 100,
      fruit: 'banana'
   };
   return this.http.post('myurl.com', data);
}

You can of course pass multiple arguments if you have parameters set up to accept them:

sendData(body: { score: number, fruit: string }) {
   return this.http.post('myurl.com', body);
}

Answered By – Super_Simon

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published