Using GET and POST service calls in same function using angular 7

Viewed 59

I am very new to Angular , Developing an app in Angular 7 . can i use the same function for post and get method?

Route.ts

{
    method: 'GET',
    path: '/api/users/getAllStudentdata/{school_code}',
    config: {
               handler: controller.getAllStudentdata,
               validate: validate.getAllStudentdata,
               tags: ['api', 'Users'],
                        auth: false,
             },
},

Some time i need to change on post method for example laravel match method

Route::match(['get', 'post'], '/', function () {
    //
});
1 Answers

You can use any API function but if : -

  1. For POST method you'll have to pass request.body because POST methods are made to insert or create data.
  2. if it is a GET API call then your function need not to have a request.body or more you can do is pass some query parameters which can fetch the data based upon your query param if there is conditional data is required, else if you require complete data this should be pretty straight forward and params would not be needed.

Examples: -

POST -> /products {must be with a Request.body}

GET -> /products {no Request.body is needed} Both are calling same function.

GET -> /products/12345 an e.g. of {/products/:id} To bring some specific data

But now you API function should be written in the way that can handle all such types of api calls.

Related