gpt4 book ai didi

javascript - 如何使用loopback 4执行post请求

转载 作者:行者123 更新时间:2023-12-03 00:04:02 25 4
gpt4 key购买 nike

经过一番研究,我发现了环回代理服务,它允许我使用 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 是函数名称。并在您的服务接口(interface)中添加相同的内容。

"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 - 如何使用loopback 4执行post请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55044968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com