Skip to content

Commit e13f52f

Browse files
PlaneInABottletest
andauthored
fix(tools): support stringified HTTP request tables (#3565)
* fix(tools): support stringified HTTP request tables Accept stored header and query tables after they are reloaded from UI JSON so HTTP requests keep their query strings and URL-encoded body handling intact. * test: mock AuthType in async execute route * test(tools): cover invalid stringified HTTP inputs --------- Co-authored-by: test <test@example.com>
1 parent e6b2b73 commit e13f52f

File tree

4 files changed

+113
-5
lines changed

4 files changed

+113
-5
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { beforeAll, describe, expect, it } from 'vitest'
2+
import { requestTool } from '@/tools/http/request'
3+
import type { RequestParams } from '@/tools/http/types'
4+
5+
beforeAll(() => {
6+
process.env.NEXT_PUBLIC_APP_URL = 'http://localhost:3000'
7+
})
8+
9+
describe('HTTP Request Tool - Stringified Params Fix', () => {
10+
it('should handle stringified params from UI storage', () => {
11+
const stringifiedParams = JSON.stringify([
12+
{ id: 'test-1', cells: { Key: 'id', Value: '311861947611' } },
13+
{ id: 'test-2', cells: { Key: 'language', Value: 'tr' } },
14+
])
15+
16+
const stringifiedHeaders = JSON.stringify([
17+
{ id: 'test-3', cells: { Key: 'Authorization', Value: 'Bearer token' } },
18+
])
19+
20+
const params = {
21+
url: 'https://api.example.com/tracking',
22+
method: 'GET' as const,
23+
params: stringifiedParams,
24+
headers: stringifiedHeaders,
25+
}
26+
27+
const url = (requestTool.request.url as (params: RequestParams) => string)(params)
28+
expect(url).toBe('https://api.example.com/tracking?id=311861947611&language=tr')
29+
30+
const headers = (
31+
requestTool.request.headers as (params: RequestParams) => Record<string, string>
32+
)(params)
33+
expect(headers.Authorization).toBe('Bearer token')
34+
})
35+
36+
it('should still handle normal array params', () => {
37+
const params = {
38+
url: 'https://api.example.com/tracking',
39+
method: 'GET' as const,
40+
params: [
41+
{ id: 'test-1', cells: { Key: 'id', Value: '311861947611' } },
42+
{ id: 'test-2', cells: { Key: 'language', Value: 'tr' } },
43+
],
44+
headers: [{ id: 'test-3', cells: { Key: 'Authorization', Value: 'Bearer token' } }],
45+
}
46+
47+
const url = (requestTool.request.url as (params: RequestParams) => string)(params)
48+
expect(url).toBe('https://api.example.com/tracking?id=311861947611&language=tr')
49+
50+
const headers = (
51+
requestTool.request.headers as (params: RequestParams) => Record<string, string>
52+
)(params)
53+
expect(headers.Authorization).toBe('Bearer token')
54+
})
55+
56+
it('should handle null and undefined params gracefully', () => {
57+
const params = {
58+
url: 'https://api.example.com/test',
59+
method: 'GET' as const,
60+
}
61+
62+
const url = (requestTool.request.url as (params: RequestParams) => string)(params)
63+
expect(url).toBe('https://api.example.com/test')
64+
65+
const headers = (
66+
requestTool.request.headers as (params: RequestParams) => Record<string, string>
67+
)(params)
68+
expect(headers).toBeDefined()
69+
})
70+
71+
it('should handle stringified object params and headers', () => {
72+
const params = {
73+
url: 'https://api.example.com/oauth/token',
74+
method: 'POST' as const,
75+
body: { grant_type: 'client_credentials' },
76+
params: JSON.stringify({ q: 'test' }),
77+
headers: JSON.stringify({ 'Content-Type': 'application/x-www-form-urlencoded' }),
78+
}
79+
80+
const url = (requestTool.request.url as (input: RequestParams) => string)(params)
81+
expect(url).toBe('https://api.example.com/oauth/token?q=test')
82+
83+
const headers = (
84+
requestTool.request.headers as (input: RequestParams) => Record<string, string>
85+
)(params)
86+
expect(headers['Content-Type']).toBe('application/x-www-form-urlencoded')
87+
88+
const body = (
89+
requestTool.request.body as (input: RequestParams) => Record<string, any> | string | FormData
90+
)(params)
91+
expect(body).toBe('grant_type=client_credentials')
92+
})
93+
94+
it('should handle invalid JSON strings gracefully', () => {
95+
const params = {
96+
url: 'https://api.example.com/test',
97+
method: 'GET' as const,
98+
params: 'not-valid-json',
99+
headers: '{broken',
100+
}
101+
102+
const url = (requestTool.request.url as (input: RequestParams) => string)(params)
103+
expect(url).toBe('https://api.example.com/test')
104+
105+
const headers = (
106+
requestTool.request.headers as (input: RequestParams) => Record<string, string>
107+
)(params)
108+
expect(headers).toBeDefined()
109+
})
110+
})

apps/sim/tools/http/request.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
7979

8080
request: {
8181
url: (params: RequestParams) => {
82-
// Process the URL once and cache the result
8382
return processUrl(params.url, params.pathParams, params.params)
8483
},
8584

@@ -115,7 +114,6 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
115114
}
116115

117116
if (params.body) {
118-
// Check if user wants URL-encoded form data
119117
const headers = transformTable(params.headers || null)
120118
const contentType = headers['Content-Type'] || headers['content-type']
121119

apps/sim/tools/http/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import type { HttpMethod, TableRow, ToolResponse } from '@/tools/types'
33
export interface RequestParams {
44
url: string
55
method?: HttpMethod
6-
headers?: TableRow[]
6+
headers?: TableRow[] | string
77
body?: unknown
8-
params?: TableRow[]
8+
params?: TableRow[] | string
99
pathParams?: Record<string, string>
1010
formData?: Record<string, string | Blob>
1111
timeout?: number

apps/sim/tools/http/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const getDefaultHeaders = (
5050
export const processUrl = (
5151
url: string,
5252
pathParams?: Record<string, string>,
53-
queryParams?: TableRow[] | null
53+
queryParams?: TableRow[] | Record<string, any> | string | null
5454
): string => {
5555
if ((url.startsWith('"') && url.endsWith('"')) || (url.startsWith("'") && url.endsWith("'"))) {
5656
url = url.slice(1, -1)

0 commit comments

Comments
 (0)