Skip to content

fix(api): convert --data to query params for GET requests#383

Merged
BYK merged 2 commits intomainfrom
fix/api-data-get-query-params
Mar 10, 2026
Merged

fix(api): convert --data to query params for GET requests#383
BYK merged 2 commits intomainfrom
fix/api-data-get-query-params

Conversation

@BYK
Copy link
Member

@BYK BYK commented Mar 10, 2026

Problem

sentry api with --data on a GET request (the default method) threw:

TypeError: fetch() request with GET/HEAD/OPTIONS method cannot have body.

The resolveBody() function always converted --data into a request body regardless of HTTP method. The Fetch API spec forbids bodies on GET/HEAD/OPTIONS requests.

Fix

When --data is used with a GET request, the data is now converted to query parameters instead of a request body:

  • URL-encoded strings (e.g. stat=received&resolution=1d) are parsed via URLSearchParams
  • JSON objects have their values stringified as query params
  • JSON arrays throw a clear ValidationError with guidance to use --method POST

This matches how --field/--raw-field already handle GET requests via prepareRequestOptions(), and aligns with gh api behavior.

Changes

  • src/commands/api.ts — Added dataToQueryParams() helper; updated resolveBody() to route --data through it for GET requests
  • test/commands/api.test.ts — 10 new tests covering the helper and resolveBody integration

Fixes CLI-CJ

@github-actions
Copy link
Contributor

github-actions bot commented Mar 10, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Init

  • Add git safety checks before wizard modifies files by betegon in #379
  • Add experimental warning before wizard runs by betegon in #378
  • Add init command for guided Sentry project setup by betegon in #283

Other

  • (issue-list) Redesign table to match Sentry web UI by BYK in #372
  • Return-based output with OutputConfig on buildCommand by BYK in #380
  • Add --fields flag for context-window-friendly JSON output by BYK in #373
  • Magic @ selectors (@latest, @most_frequent) for issue commands by BYK in #371
  • Input hardening against agent hallucinations by BYK in #370
  • Add response caching for read-only API calls by BYK in #330

Bug Fixes 🐛

Init

  • Remove implementation detail from help text by betegon in #385
  • Truncate uncommitted file list to first 5 entries by MathurAditya724 in #381

Other

  • (api) Convert --data to query params for GET requests by BYK in #383
  • (docs) Remove double borders and fix column alignment on landing page tables by betegon in #369
  • Add trace ID validation to trace view + UUID dash-stripping by BYK in #375

Internal Changes 🔧

Init

  • Remove --force flag by betegon in #377
  • Remove dead determine-pm step label by betegon in #374

Other

  • Converge Tier 1 commands to writeOutput helper by BYK in #376

🤖 This preview updates automatically when you update the PR.

@BYK BYK marked this pull request as ready for review March 10, 2026 17:01
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

BYK added 2 commits March 10, 2026 18:16
When using `sentry api` with `--data` and a GET method (the default),
the data was always sent as a request body. The Fetch API spec forbids
bodies on GET/HEAD/OPTIONS requests, causing:

  TypeError: fetch() request with GET/HEAD/OPTIONS method cannot have body.

Now `--data` on GET requests converts the data to query parameters:
- URL-encoded strings (e.g. `stat=received&resolution=1d`) are parsed
  via URLSearchParams
- JSON objects have their values stringified as query params
- JSON arrays throw a clear ValidationError with guidance

This matches how `--field`/`--raw-field` already handle GET requests
via `prepareRequestOptions()`, and aligns with `gh api` behavior.

Fixes CLI-CJ
parseDataBody can return null, booleans, or numbers from JSON.parse
(hidden behind an `as` cast). These fall through the string and array
checks in dataToQueryParams and crash on Object.entries(null).

Guard against all non-object primitives with a clear ValidationError.
Extract URL-encoded parsing to a helper to stay under complexity limit.
@BYK BYK force-pushed the fix/api-data-get-query-params branch from 5db9a4d to 31e7dfd Compare March 10, 2026 18:17
@github-actions
Copy link
Contributor

Codecov Results 📊

104 passed | Total: 104 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 899 uncovered lines.
✅ Project coverage is 95.49%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    95.48%    95.49%    +0.01%
==========================================
  Files          142       142         —
  Lines        19898     19933       +35
  Branches         0         0         —
==========================================
+ Hits         18999     19034       +35
- Misses         899       899         —
- Partials         0         0         —

Generated by Codecov Action

@BYK BYK merged commit 9a02837 into main Mar 10, 2026
20 checks passed
@BYK BYK deleted the fix/api-data-get-query-params branch March 10, 2026 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant