Skip to content

Commit b51bdaa

Browse files
authored
fix: custom api does not work when step parameter is the JSON body itself (#1281)
## Problem Custom API body does not work when the step parameter is the body. The computed variable from the step parameter is not being parsed properly. ## Solution * Attempt to parse the variable as a valid JSON first * Only strip the extra " from the start and end of the string if its not a valid JSON as the user would have already wrapped it in " ## How to test? - [ ] Works when body is a manually configured JSON, i.e., variables are used as the key or value in the JSON object - [ ] Works when the body is a JSON object from a step variable
1 parent 2849ea7 commit b51bdaa

File tree

1 file changed

+13
-0
lines changed
  • packages/backend/src/apps/custom-api/actions/http-request

1 file changed

+13
-0
lines changed

packages/backend/src/apps/custom-api/actions/http-request/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,21 @@ const action: IRawAction = {
9191

9292
preprocessVariable(parameterKey: string, variableValue: unknown) {
9393
if (parameterKey === 'data' && typeof variableValue === 'string') {
94+
// check if the variable is a valid JSON
95+
if (variableValue.startsWith('{') || variableValue.endsWith('}')) {
96+
try {
97+
JSON.parse(variableValue as string)
98+
// its a valid JSON, don't need to do anything extra
99+
return variableValue
100+
} catch (e) {
101+
// not a valid JSON, remove the " from the start and end of the string
102+
return JSON.stringify(variableValue).slice(1, -1)
103+
}
104+
}
94105
// NOTE: this removes the " from the start and end of the string
95106
// as it is already added in the user input
107+
// when user forms the JSON on the frontend:
108+
// {"key": "<variable>"}
96109
return JSON.stringify(variableValue).slice(1, -1)
97110
}
98111
return variableValue

0 commit comments

Comments
 (0)