mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-04 03:35:04 -05:00
fix(http): serialize nested objects in form-urlencoded body (#3124)
This commit is contained in:
@@ -286,6 +286,30 @@ describe('HTTP Request Tool', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('should handle nested objects and arrays in URL-encoded form data', async () => {
|
||||
tester.setup({ result: 'success' })
|
||||
|
||||
const body = {
|
||||
name: 'test',
|
||||
data: { nested: 'value' },
|
||||
items: [1, 2, 3],
|
||||
}
|
||||
|
||||
await tester.execute({
|
||||
url: 'https://api.example.com/submit',
|
||||
method: 'POST',
|
||||
body,
|
||||
headers: [{ cells: { Key: 'Content-Type', Value: 'application/x-www-form-urlencoded' } }],
|
||||
})
|
||||
|
||||
const fetchCall = (global.fetch as any).mock.calls[0]
|
||||
const bodyStr = fetchCall[1].body
|
||||
|
||||
expect(bodyStr).toContain('name=test')
|
||||
expect(bodyStr).toContain('data=%7B%22nested%22%3A%22value%22%7D')
|
||||
expect(bodyStr).toContain('items=%5B1%2C2%2C3%5D')
|
||||
})
|
||||
|
||||
it('should handle OAuth client credentials requests', async () => {
|
||||
tester.setup({ access_token: 'token123', token_type: 'Bearer' })
|
||||
|
||||
|
||||
@@ -105,7 +105,10 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
|
||||
const urlencoded = new URLSearchParams()
|
||||
Object.entries(params.body as Record<string, unknown>).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
urlencoded.append(key, String(value))
|
||||
urlencoded.append(
|
||||
key,
|
||||
typeof value === 'object' ? JSON.stringify(value) : String(value)
|
||||
)
|
||||
}
|
||||
})
|
||||
return urlencoded.toString()
|
||||
|
||||
Reference in New Issue
Block a user