javascript - 如何使用环回 4 执行发布请求
问题描述
经过一番研究,我发现了环回代理服务,它允许我使用 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);
}
}
解决方案
在您的数据源文件中,在操作数组中为您的 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"
]
}
}]
推荐阅读
- javascript - 内部带有按钮或跨度的输入字段
- php - Laravel 路由发布方法失败
- php - Laravel 资源 - 有孩子的父母类别
- rinsim - CommRenderer 无法处理 commUser.getPosition() 中的缺失值
- python - python嵌套用于打印数据框中列的位置和名称
- bing - 如何在 lynx 命令行 bing/google 搜索查询中包含“+”(必须包含)搜索运算符
- javascript - mosquitto 1.4.14 不支持 Windows 中的 Web 套接字并且代码不起作用
- arduino - Arduino代码不会上传
- picasso - 无法使用(android.content.context)解析方法?
- entity-framework - 自实体框架更新以来的奇怪问题