From c268739e1f6d160f60113f46daf7f6bc2897b512 Mon Sep 17 00:00:00 2001 From: Derek Stotz Date: Fri, 16 Feb 2024 22:25:31 -0600 Subject: [PATCH] Enhancement: Add requestBody param for customapi (#2921) --- docs/widgets/services/customapi.md | 14 ++++++++++++++ src/utils/proxy/handlers/generic.js | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/docs/widgets/services/customapi.md b/docs/widgets/services/customapi.md index 29b72633e..7f26f80fb 100644 --- a/docs/widgets/services/customapi.md +++ b/docs/widgets/services/customapi.md @@ -16,6 +16,7 @@ widget: password: password # auth - optional method: GET # optional, e.g. POST headers: # optional, must be object, see below + requestBody: # optional, can be string or object, see below display: # optional, default to block, see below mappings: - field: key # needs to be YAML string or object @@ -166,3 +167,16 @@ Pass custom headers using the `headers` option, for example: headers: X-API-Token: token ``` + +## Custom Request Body + +Pass custom request body using the `requestBody` option in either a string or object format. Objects will automatically be converted to a JSON string. + +```yaml +requestBody: + foo: bar +# or +requestBody: "{\"foo\":\"bar\"}" +``` + +Both formats result in `{"foo":"bar"}` being sent as the request body. Don't forget to set your `Content-Type` headers! diff --git a/src/utils/proxy/handlers/generic.js b/src/utils/proxy/handlers/generic.js index 8b91049fd..e4717469a 100644 --- a/src/utils/proxy/handlers/generic.js +++ b/src/utils/proxy/handlers/generic.js @@ -35,6 +35,12 @@ export default async function genericProxyHandler(req, res, map) { }; if (req.body) { params.body = req.body; + } else if (widget.requestBody) { + if (typeof widget.requestBody === "object") { + params.body = JSON.stringify(widget.requestBody); + } else { + params.body = widget.requestBody; + } } const [status, contentType, data] = await httpProxy(url, params);