如何使用环回4执行发布请求

时间:2019-03-07 13:28:08

标签: javascript loopback new-webserviceproxy

经过研究,我发现了环回代理服务,这使我可以使用get方法进行一些查询。但是我不知道如何使用请求正文中的数据执行POST请求。

我的服务

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);
  }
}

2 个答案:

答案 0 :(得分:1)

在数据源文件中,为Operations数组内的post函数添加一个模板,其中postFunction是函数名。并在您的服务界面中添加相同的内容。

"operations": [{
      "template": {
        "method": "POST",
        "url": "{POST-API-URL}",
        "headers": {
          "accepts": "application/json",
          "content-type": "application/json"
        },
        "json": {
          "param1": "{value1}",
          "param2": "{value2}",
          "param3": "{value3}"
        }
      },
      "functions": {
        "postFunction": [
          "value1",
          "value2",
          "value3"
        ]
      }
    }]

答案 1 :(得分:0)

在深深的苦恼之后,我终于找到了答案。我们只需要将方法定义为POST并保留一些参数,这些参数将用作请求表单参数的变量。 希望它对这里的人有所帮助。

 {
  "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"]
    }
 }
]
}