Skip to content

Add an example to gh api --help demonstrating usage of --input #9104

Closed

Description

@williammartin

Description

As mentioned in #8983, when it becomes challenging to use -f and -F to construct the exact JSON request body for gh api, the best option is to fallback to constructing JSON externally and to pass that via the --input flag.

#8983 (comment) suggests that we add to the example section of gh api --help to make this more obvious, and that seems like a great idea. The code to change is here:

Example: heredoc.Doc(`

Activity

  1. added
    help wanted
    gh-api
    on May 21, 2024
  2. arifbalik commented on Aug 8, 2024

    @arifbalik

    Hey @williammartin

    I recently came across this issue and was bummed that it was not on the docs, so I'm adding it now, but I have a few questions;

    First I've only added two lines, do you think i should also write some tests or this is already tested?

    diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go
    index c8eac911..f6e3c5d1 100644
    --- a/pkg/cmd/api/api.go
    +++ b/pkg/cmd/api/api.go
    @@ -156,6 +156,9 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
                               -F 'properties[][allowed_values][]=staging' \
                               -F 'properties[][allowed_values][]=production'
     
    +                       # pass an external JSON file as the request body
    +                       $ gh api repos/{owner}/{repo}/rulesets --input my-request-body.json
    +
                            # list releases with GraphQL
                            $ gh api graphql -F owner='{owner}' -F name='{repo}' -f query='
                              query($name: String!, $owner: String!) {

    Also this comment gives a much better example but i did not want to bloat the examples like that. If thats not a problem I can propose a change like this;

    index c8eac911..10cd5d7f 100644
    --- a/pkg/cmd/api/api.go
    +++ b/pkg/cmd/api/api.go
    @@ -156,6 +156,39 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
                               -F 'properties[][allowed_values][]=staging' \
                               -F 'properties[][allowed_values][]=production'
    
    +                       # pass a JSON string as the request body
    +                       echo '{
    +                       "name": "example ruleset",
    +                       "target": "branch",
    +                       "enforcement": "active",
    +                       "conditions": {
    +                               "ref_name": {
    +                               "exclude": [],
    +                               "include": [
    +                                       "~DEFAULT_BRANCH"
    +                               ]
    +                               }
    +                       },
    +                       "rules": [
    +                               {
    +                               "type": "deletion"
    +                               },
    +                               {
    +                               "type": "pull_request",
    +                               "parameters": {
    +                                       "required_approving_review_count": 1,
    +                                       "dismiss_stale_reviews_on_push": true,
    +                                       "require_code_owner_review": false,
    +                                       "require_last_push_approval": true,
    +                                       "required_review_thread_resolution": true
    +                               }
    +                               }
    +                       ]
    +                       }'  | gh api repos/{owner}/{repo}/rulesets  --input -
    +
    +                       # pass an external JSON file as the request body
    +                       $ gh api repos/{owner}/{repo}/rulesets --input my-request-body.json
    +
                            # list releases with GraphQL
                            $ gh api graphql -F owner='{owner}' -F name='{repo}' -f query='
                              query($name: String!, $owner: String!) {
  3. removed
    help wanted
    on Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsgh-api

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions