Search code examples
javascriptloopbacknew-webserviceproxy

How to perform a post request with loopback 4


After some research I found the loopback proxy sevices which allowed me to do some queries with the get method. But I do not know how I can perform a POST request with the data in the body of the request.

my service

import {getService} from '@loopback/service-proxy';
import {inject, Provider} from '@loopback/core';
import {StorageApiDataSource} from '../datasources';
/**
 * user for  both storageApi service and authApi service
 */
export interface StorageApiService {
  // this is where you define the Node.js methods that will be
  // mapped to the SOAP operations as stated in the datasource
  // json file.

  getrestdata(id?: number): Promise<StorageApiResponseData>;
  saveFile(token?: string,requestBody:any): Promise<StorageApiResponseData>;

}
export interface StorageApiResponseData {
  file_id: string;
  direct_url: string;
}

export class StorageApiServiceProvider implements Provider<StorageApiService> {
  constructor(
    // storageApi must match the name property in the datasource json file
    @inject('datasources.storageApi')
    protected dataSource: StorageApiDataSource = new StorageApiDataSource(),
  ) {}

  value(): Promise<StorageApiService> {
    return getService(this.dataSource);
  }
}


Solution

  • i finally find an answer after deep seaches. We just need to define method as POST and past some params which will be used as variables for thes request form params. hope it help somebody here.

     {
      "name": "SendNotif",
        "connector": "rest",
        "baseURL": "",
        "crud": false,
       "options": {
       "headers": {
               "accept": "application/json",
               "authorization": " MY AUTH KEY",
                "content-type": "application/json"
                  }
      },
    "operations": [
      { "template": {
         "method": "GET",
         "url":"MY URL"
    
         },
         "functions": {
          "send_sms": ["PARAM1","PARAM2","PARAM3"]
        },
       "template": {
         "method": "POST",
         "url":"MY URL"
    
         },
         "functions": {
          "send_sms":["PARAM1","PARAM2","PARAM3"]
        }
     }
    ]
    }