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

JSON in body of request #132

Open
aarontwf opened this issue Dec 6, 2021 · 5 comments
Open

JSON in body of request #132

aarontwf opened this issue Dec 6, 2021 · 5 comments
Labels
Type: Documentation Improvements or additions to documentation Type: Support Any questions, information, or general needs around the SDK or GitHub APIs

Comments

@aarontwf
Copy link

aarontwf commented Dec 6, 2021

Hey, just wondering how JSON is supposed to be sent?
Tried a few things but getting different errors back.

This is the API request I'm trying https://docs.github.com/en/rest/reference/issues#create-a-label

    steps:
      - uses: octokit/[email protected]
        with:
          route: POST /repos/${{ github.repository }}/labels
          body: "'\\'{\"name\":\"React ${{ github.event.ref }}\"}\\''"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Error: end of the stream or a document separator is expected (1:4)

 1 | '\'{"name":"React aarontwf-patch-2"}\''
--------^

body: "''{\"name\":\"React ${{ github.event.ref }}\"}''"
Error: end of the stream or a document separator is expected (1:3)

 1 | ''{"name":"React aarontwf-patch-1"}''
-------^

body: "'{\"name\":\"React ${{ github.event.ref }}\"}'"
Error: Invalid request.

"name" wasn't supplied.

body: '{"name":"React ${{ github.event.ref }}"}'
Error: Invalid request.

"name" wasn't supplied.
@dsoegijono
Copy link

dsoegijono commented Jan 7, 2022

Try putting it in with directly instead of body:

steps:
      - uses: octokit/[email protected]
        with:
          route: POST /repos/${{ github.repository }}/labels
          name: React ${{ github.event.ref }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

I ran into the same issue for another endpoint. I think the documentation is not very clear on how to pass payload.

@nickfloyd nickfloyd added Type: Documentation Improvements or additions to documentation Type: Support Any questions, information, or general needs around the SDK or GitHub APIs labels Jun 30, 2022
@dcarbone
Copy link

For anybody else who comes across this, if you wish to make a request with a JSON body containing nested values it must be done something like this:

    steps:
      - name: 'Construct Github Environment create / update request'
        id: gh_env_req
        run: |
          _reviewers_json="$(cat <<EOF
          [ { "type": "Team", "id": 0 } ]
          EOF
          )"
          
          echo "::set-output name=reviewers_json::${_reviewers_json}"

      - name: Create / Update Github environment
        uses: octokit/[email protected]
        with:
          route: 'PUT /repos/{repo_path}/environments/{env_name}'
          repo_path: '${{ github.repository }}'
          env_name: my-env
          reviewers: '${{ steps.gh_env_req.outputs.reviewers_json }}'

If your request is static, you should probably be able to define the json contents of the nested field directly into the with: block, but for my specific use-case the json is actually constructed with the output of a subsequent api call to get the ID of the reviewer's team, thus necessitating an additional step.

The heredoc syntax and spaces between json tokens are purely for readability and are not required.

@timrogers
Copy link
Contributor

@dcarbone 👋🏻 Would you be up for making a PR to improve the documentation? I'm sure other people will run into issues like this.

@dcarbone
Copy link

Sure, i'll work on one in a bit.

@Nomango
Copy link

Nomango commented Dec 20, 2023

Constructing a JSON body in with can be challenging. But I have found a simpler way to invoke the GitHub API. actions/github-script looks pretty good.

    steps:
      - id: get_release_notes
        uses: actions/github-script@v7
        with:
          # see syntax on https://octokit.github.io/rest.js/v20
          script: |
            const { data } = await github.rest.repos.generateReleaseNotes({
              owner: '${{ github.repository_owner }}',
              repo: '${{ github.event.repository.name }}',
              tag_name: 'v1.0.0',
            });
            core.setOutput('data', data);

      - name: Show release note
        run: 'echo "${{ fromJSON(steps.get_release_notes.outputs.data).body }}"'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Documentation Improvements or additions to documentation Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
Status: 🔥 Backlog
Development

No branches or pull requests

6 participants