Repository navigation
Allow nil request body for non-GET request #3937
Description
Activity
- addedneeds-designAn engineering task needs design to proceedAn engineering task needs design to proceed
on Jul 12, 2021 I'm fine with
nilinstead of{}when no fields would be sent, but off the top of my head can't predict if that will break other endpoints which might expect an empty object instead ofnil.If someone is up for an exploratory PR + QA to see how well that works that would be great.
I think it should be safe to send an empty body instead of
{}, even though it would be a backwards-incompatible change. Based on my informal inspection of the GitHub API, the endpoints that expect no arguments don't enforce that the input object must be{}. But, I'd need to take a deeper dive to confirm.As a potential workaround for this problem until a gh release that fixes this, I think the following might work:
echo -n | gh api --method DELETE path/to/endpoint --input -I think it should be safe to send an empty body instead of
{}, even though it would be a backwards-incompatible change. Based on my informal inspection of the GitHub API, the endpoints that expect no arguments don't enforce that the input object must be{}. But, I'd need to take a deeper dive to confirm.As a potential workaround for this problem until a gh release that fixes this, I think the following might work:
echo -n | gh api --method DELETE path/to/endpoint --input -I confirm that this fixes the issue, at least for the /packages/container/x/versions/x endpoint with
DELETE.Reacted by Alessandro PantanoFrom my cursory inspection of the JSON schema of our REST API endpoints, I do not think that sending an empty body instead of
{}will break anything. This is now open to contributions — your starting point could be here:Lines 44 to 48 in a6f6ad7
b, err := json.Marshal(pp) if err != nil { return nil, fmt.Errorf("error serializing parameters: %w", err) } body = bytes.NewBuffer(b) If
len(pp) == 0, we could use an empty body instead of allowing the body to be encoded as{}in JSON.- addedpriority-2Affects more than a few users but doesn't prevent core functionsAffects more than a few users but doesn't prevent core functionshelp wantedContributions welcomeContributions welcomeand removedneeds-designAn engineering task needs design to proceedAn engineering task needs design to proceed
on Mar 31, 2022 If anyone is still suffering from this problem, here is a working solution:
gh api --method DELETE --input /dev/null "/orgs/$ORG_NAME/packages/container/$PACKAGE_NAME/versions/$VERSION_ID"Reacted by Stu Mace and KuroxIf anyone is still suffering from this problem, here is a working solution:
gh api --method DELETE --input /dev/null "/orgs/$ORG_NAME/packages/container/$PACKAGE_NAME/versions/$VERSION_ID"I think this should be in the docs!
Describe the feature or problem you’d like to solve
We experienced an issue when trying to delete a package version via the GH API using the cli:
This happens because the CLI tries to send
{}in the absence of any other definedfields for a request with a method other than GET. This is an issue for this endpoint (and possibly others) that use validators such as JSON-schema that mandate a request body be null.Proposed solution
There are many ways to handle this including:
--no-fields).fieldsuch as "" that the CLI understands to mean give backnil(probably not advised)But I think the optimal solution is the one that would be the most intuitive and least friction to users: assume
niland not{}when no fields are given.I believe the problem spot lies at:
cli/pkg/cmd/api/api.go
Line 460 in a6710ec
Though it may take some refactoring elsewhere it could be as simple as having
paramsreturned as nil if no fields were parsed out.Additional context
cURL example showing the same error: