Skip to content

fix: print empty and non-JSON api responses correctly - #78

Merged
ramseydsilva merged 2 commits into
equinix:mainfrom
lbarrosby:fix/api-non-json-response
Nov 17, 2026
Merged

ramseydsilva merged 2 commits into
equinix:mainfrom
lbarrosby:fix/api-non-json-response

Conversation

@lbarrosby

Copy link
Copy Markdown
Contributor

equinix api printed null for any response that was not JSON. In cmd/api.go the error from json.Unmarshal(resp, &raw) was overwritten by the following json.MarshalIndent(raw, ...), which happily marshals the nil raw:

$ equinix api -X DELETE fabric/v4/routeFilters/<id>      # 204 No Content
null
$ equinix api <path returning text/plain "OK">
null

So a successful DELETE looked like it returned something, and a text or HTML body (an error page from a proxy, a health endpoint) was lost.

Fix

  • An empty (or whitespace-only) body prints nothing.
  • A body that does not parse as JSON is printed as it came back.
  • JSON keeps going through the --format json|yaml path unchanged.

Testing

cmd/api_output_test.go adds TestE2E_APIEmptyResponsePrintsNothing and TestE2E_APINonJSONResponseIsPrintedAsIs. They run the real root command against a local fake API (redirectTransport and runCLI from e2e_test.go). Both print "null\n" on main and fail; with the fix they pass. go test ./..., go vet ./... and golangci-lint run ./cmd/ are clean.

This is separate from the --data request body fix and touches a different file, so the two can be merged in either order.

AI disclosure: written with the help of Claude Code, reviewed and tested by me.

equinix api ignored the json.Unmarshal error and re-marshalled the nil
value, so a 204 No Content or a plain-text body was printed as "null".
Print nothing for an empty body and the body as it is when it is not
JSON.

Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>

@ramseydsilva ramseydsilva left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The fix's shape is right: early-return on empty, pass through on unparseable, leave the JSON path untouched.

@ramseydsilva

Copy link
Copy Markdown
Contributor

Reviewed this by building the branch and running it, not just reading the diff. The diagnosis is exact: on main the json.Unmarshal error is overwritten by json.MarshalIndent(raw, ...), which marshals the nil raw to null. I copied cmd/api_output_test.go onto main and both tests fail with output = "null\n"; on this branch they pass. So the tests genuinely pin the behavior rather than describing the implementation. go test ./... and go vet ./... are clean here too.

The shape of the fix looks right to me — early-return on an empty body, pass through on an unparseable one, leave the JSON path untouched. Two small, non-blocking notes:

  • fmt.Println(string(resp)) appends a newline unconditionally, so a text/plain body that already ends in \n prints a trailing blank line. os.Stdout.Write(resp) plus a newline only when the body doesn't end in one would be tighter. The test uses "OK" with no trailing newline, so it doesn't cover this.
  • The description cites "an error page from a proxy" as a body that was lost, but that case isn't reachable: internal/api/api.go:285 turns any status >= 400 into an error and returns before reading the body, so non-JSON bodies only reach this code on a 2xx/3xx. The health-endpoint example is accurate. Worth adjusting so the commit message doesn't mislead later.

I also filed #88 for two adjacent "silently discards output" bugs that are out of scope here: --format is unvalidated (so --format xml prints a blank line and exits 0), and the API's structured error body is dropped on 4xx/5xx. Both are independent of this PR.

AI disclosure: reviewed with the help of Claude Code.

@ramseydsilva
ramseydsilva merged commit c91e678 into equinix:main Nov 17, 2026
6 checks passed
@github-actions

Copy link
Copy Markdown

This PR is included in version 0.6.0 🎉

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.

2 participants