The gh CLI is a powerful tool for interacting with GitHub from the command line. It allows you to manage repositories, issues, pull requests, and more without leaving your terminal. However, when things do not work as expected, it can be challenging to understand what is happening under the hood.
Sometimes, you may encounter cryptic errors like a 403 Forbidden response. This error can occur for various reasons: insufficient permissions, rate limiting, or authentication issues. In such cases, enabling debug logging can provide valuable insights into the actual HTTP requests and responses.
#Enabling debug logging
To enable debug logging in the gh CLI, set the GH_DEBUG environment variable to 1 or api. This will output detailed information about the HTTP requests being made, including headers, request bodies, and response data.
On Linux or macOS:
Shell
export GH_DEBUG=1
On Windows (PowerShell):
PowerShell
$env:GH_DEBUG = "api"
After setting this environment variable, any subsequent gh commands will include debug information in their output.
Let's say you want to list releases for a repository. With debug logging enabled, running:
Shell
gh release list
The debug output shows:
- The timestamp of the request
- The full URL being called, including query parameters
- The headers sent with the request
- The request body
- The HTTP status code returned
- The response headers
- The response body
When an error occurs, you will also see the response body, which often contains more details about why the request failed. For example, a 403 error might reveal whether you hit a rate limit or lack the necessary permissions.
Do you have a question or a suggestion about this post? Contact me!