Curl Github



Article version: GitHub.com
  • Instantly convert curl commands to Java code. This tool turns a curl command into Java Apache HttpClient Fluent code. Currently, it knows the following options: -d/-data, -H/-header, -I/-head, -url, and -X/-request. Feel free to contribute on GitHub! This script derives from Matt Holt's excellent curl-to-Go and curl-to-php.
  • After referring this guide I needed to access the github graphql by using curl for a testing purpose. I tried this simple command curl -i -H 'Authorization: bearer myGithubAccessToken' -X POST -d.
Article version: GitHub.com
Curl

On the CI server, I want to fetch a config file that we maintain on Github so it can be shared between many jobs. I'm trying to get this file via curl, but these approaches both fail (I get a 404). GitHub - curl/curl: A command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. Libcurl offers a myriad of powerful features. CURLpp is a C wrapper for libcURL. LibcURL is described as. A free and easy-to-use client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP. Libcurl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, kerberos, HTTP form based upload, proxies, cookies, user+password authentication, file transfer resume, http proxy.

Learn the foundations for using the REST API, starting with authentication and some endpoint examples.

In this article

Let's walk through core API concepts as we tackle some everyday use cases.

Overview

Most applications will use an existing wrapper library in the languageof your choice, but it's important to familiarize yourself with the underlying APIHTTP methods first.

There's no easier way to kick the tires than through cURL. If you are usingan alternative client, note that you are required to send a validUser Agent header in your request.

Hello World

Let's start by testing our setup. Open up a command prompt and enter thefollowing command:

The response will be a random selection from our design philosophies.

Next, let's GETChris Wanstrath'sGitHub profile:

Mmmmm, tastes like JSON. Let's add the -i flag to include headers:

There are a few interesting bits in the response headers. As expected, theContent-Type is application/json.

Any headers beginning with X- are custom headers, and are not included in theHTTP spec. For example:

  • X-GitHub-Media-Type has a value of github.v3. This lets us know the media typefor the response. Media types have helped us version our output in API v3. We'lltalk more about that later.
  • Take note of the X-RateLimit-Limit and X-RateLimit-Remaining headers. Thispair of headers indicate how many requests a client can make ina rolling time period (typically an hour) and how many of those requests theclient has already spent.

Authentication

Unauthenticated clients can make 60 requests per hour. To get more requests per hour, we'll need toauthenticate. In fact, doing anything interesting with the GitHub API requiresauthentication.

Using personal access tokens

The easiest and best way to authenticate with the GitHub API is by using Basic Authentication via OAuth tokens. OAuth tokens include personal access tokens.

Use a -u flag to set your username:

When prompted, you can enter your OAuth token, but we recommend you set up a variable for it:

You can use -u 'username:$token' and set up a variable for token to avoid leaving your token in shell history, which should be avoided.

When authenticating, you should see your rate limit bumped to 5,000 requests an hour, as indicated in the X-RateLimit-Limit header. In addition to providing more calls per hour, authentication enables you to read and write private information using the API.

You can easily create a personal access token using your Personal access tokens settings page:

Get your own user profile

When properly authenticated, you can take advantage of the permissionsassociated with your GitHub account. For example, try gettingyour own user profile:

This time, in addition to the same set of public information weretrieved for @defunkt earlier, you should also see the non-public information for your user profile. For example, you'll see a plan object in the response which gives details about the GitHub plan for the account.

Using OAuth tokens for apps

Apps that need to read or write private information using the API on behalf of another user should use OAuth.

OAuth uses tokens. Tokens provide two big features:

  • Revokable access: users can revoke authorization to third party apps at any time
  • Limited access: users can review the specific access that a tokenwill provide before authorizing a third party app

Tokens should be created via a web flow. An applicationsends users to GitHub to log in. GitHub then presents a dialogindicating the name of the app, as well as the level of access the apphas once it's authorized by the user. After a user authorizes access, GitHubredirects the user back to the application:

Treat OAuth tokens like passwords! Don't share them with other users or storethem in insecure places. The tokens in these examples are fake and the names havebeen changed to protect the innocent.

Now that we've got the hang of making authenticated calls, let's move along tothe Repositories API.

Repositories

Almost any meaningful use of the GitHub API will involve some level of Repositoryinformation. We can GET repository details in the same way we fetched userdetails earlier:

In the same way, we can view repositories for the authenticated user:

Or, we can list repositories for another user:

Or, we can list repositories for an organization:

The information returned from these calls will depend on which scopes our token has when we authenticate:

  • A token with public_reposcope returns a response that includes all public repositories we have access to see on github.com.
  • A token with reposcope returns a response that includes all public and private repositories we have access to see on GitHub.

As the docs indicate, these methods take a type parameter thatcan filter the repositories returned based on what type of access the user hasfor the repository. In this way, we can fetch only directly-owned repositories,organization repositories, or repositories the user collaborates on via a team.

In this example, we grab only those repositories that octocat owns, not theones on which she collaborates. Note the quoted URL above. Depending on yourshell setup, cURL sometimes requires a quoted URL or else it ignores thequery string.

Create a repository

Fetching information for existing repositories is a common use case, but theGitHub API supports creating new repositories as well. To create a repository,we need to POST some JSON containing the details and configuration options.

In this minimal example, we create a new private repository for our blog (to be servedon GitHub Pages, perhaps). Though the blog will be public, we've made the repository private. In this single step, we'll also initialize it with a README and a nanoc-flavored .gitignore template.

The resulting repository will be found at https://github.com/<your_username>/blog.To create a repository under an organization for which you'rean owner, just change the API method from /user/repos to /orgs/<org_name>/repos.

Next, let's fetch our newly created repository:

Oh noes! Where did it go? Since we created the repository as private, we needto authenticate in order to see it. If you're a grizzled HTTP user, you mightexpect a 403 instead. Since we don't want to leak information about privaterepositories, the GitHub API returns a 404 in this case, as if to say 'we canneither confirm nor deny the existence of this repository.'

Issues

The UI for Issues on GitHub aims to provide 'just enough' workflow whilestaying out of your way. With the GitHub Issues API, you can pulldata out or create issues from other tools to create a workflow that works foryour team.

Just like github.com, the API provides a few methods to view issues for theauthenticated user. To see all your issues, call GET /issues:

To get only the issues under one of your GitHub organizations, call GET /orgs/<org>/issues:

We can also get all the issues under a single repository:

Pagination

A project the size of Rails has thousands of issues. We'll need to paginate,making multiple API calls to get the data. Let's repeat that last call, thistime taking note of the response headers:

The Link header provides a way for a response to link toexternal resources, in this case additional pages of data. Since our call foundmore than thirty issues (the default page size), the API tells us where we canfind the next page and the last page of results.

Creating an issue

Now that we've seen how to paginate lists of issues, let's create an issue fromthe API.

Curl Syntax

To create an issue, we need to be authenticated, so we'll pass anOAuth token in the header. Also, we'll pass the title, body, and labels in the JSONbody to the /issues path underneath the repository in which we want to createthe issue:

The response gives us a couple of pointers to the newly created issue, both inthe Location response header and the url field of the JSON response.

Github

Conditional requests

A big part of being a good API citizen is respecting rate limits by caching information that hasn't changed. The API supports conditionalrequests and helps you do the right thing. Consider thefirst call we made to get defunkt's profile:

In addition to the JSON body, take note of the HTTP status code of 200 andthe ETag header.The ETag is a fingerprint of the response. If we pass that on subsequent calls,we can tell the API to give us the resource again, only if it has changed:

The 304 status indicates that the resource hasn't changed since the last timewe asked for it and the response will contain no body. As a bonus, 304 responses don't count against your rate limit.

Woot! Now you know the basics of the GitHub API!

  • Basic & OAuth authentication
  • Fetching and creating repositories and issues
  • Conditional requests

Curl Github Api Examples

Keep learning with the next API guide Basics of Authentication!