Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove body from REST call if no body elements are defined #841

Merged
merged 2 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/run-it/src/utils/RunItSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { RunItConfigKey } from '../components'
const settings = {
...DefaultSettings(),
base_url: 'https://self-signed.looker.com:19999',
agentTag: 'RunIt 0.5',
agentTag: 'RunIt 0.8',
} as IApiSettings

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/run-it/src/utils/requestUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ describe('requestUtils', () => {
}),
}

const noBody = {
result_format: 'json',
cache: true,
body: '{}',
}

test('empty json body is removed', () => {
const [pathParams, queryParams, body] = createRequestParams(
inputs,
noBody
)
expect(pathParams).toEqual({
result_format: noBody.result_format,
})
expect(queryParams).toEqual({
cache: noBody.cache,
})
expect(body).not.toBeDefined()
})

test('it correctly identifies requestContent params location', () => {
const [pathParams, queryParams, body] = createRequestParams(
inputs,
Expand Down
10 changes: 9 additions & 1 deletion packages/run-it/src/utils/requestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ export const prepareInputs = (
const name = input.name
if (input.location === 'body') {
try {
result[name] = JSON.parse(result[name])
const parsed = JSON.parse(result[name])
// Keys call works for both objects and arrays if there are any values
const keys = Object.keys(parsed)
if (keys.length > 0) {
result[name] = parsed
} else {
// Remove body arg
delete result[name]
}
} catch (e) {
/** Treat as x-www-form-urlencoded */
result[name] = requestContent[name]
Expand Down