Skip to content

Instantly share code, notes, and snippets.

@adityaarakeri
Forked from btoone/curl.md
Created March 20, 2019 06:39
Show Gist options
  • Save adityaarakeri/0fd5d1083d784ff47b6d128e7ab86ddf to your computer and use it in GitHub Desktop.
Save adityaarakeri/0fd5d1083d784ff47b6d128e7ab86ddf to your computer and use it in GitHub Desktop.

Revisions

  1. J. Voigt revised this gist Mar 30, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions curl.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Introduction #

    An introduction to curl using GitHub's API
    An introduction to [`curl`](http://curl.haxx.se/) using [GitHub's API](https://developer.github.com/guides/getting-started/#overview).

    # The Basics #

    @@ -101,4 +101,4 @@ List the authorizations you already have
    # Resources #

    * HTTParty - Ruby library that makes it easy to create HTTP requests https://github.com/jnunemaker/httparty
    * Hurl IT - An open source web application to play with curl options http://hurl.it
    * Hurl IT - An open source web application to play with curl options http://hurl.it
  2. Brandon Toone revised this gist Oct 2, 2013. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions curl.md
    Original file line number Diff line number Diff line change
    @@ -17,27 +17,27 @@ Pass user credential to basic auth to access protected resources like a users st
    curl --user "caspyin:PASSWD" https://api.github.com/gists/starred
    curl --user "caspyin:PASSWD" https://api.github.com/users/caspyin

    Passing just the username without the colon(:) will cause you to be prompted for your account password. This avoids having your password in your command line history
    Passing just the username without the colon (`:`) will cause you to be prompted for your account password. This avoids having your password in your command line history

    curl --user "caspyin" https://api.github.com/users/caspyin

    ## POST ##

    Use the --request (-X) flag along with --data (-d) to POST data
    Use the `--request` (`-X`) flag along with `--data` (`-d`) to POST data

    curl --user "caspyin" --request POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

    curl --user "caspyin" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

    Of course --data implies POST so you don't have to also specify the --request flag
    Of course `--data` implies POST so you don't have to also specify the `--request` flag

    curl --user "caspyin" --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

    Here is an example that uses the old GitHub API (v2). You can use multiple --data flags
    Here is an example that uses the old GitHub API (v2). You can use multiple `--data` flags

    curl --data "login=caspyin" --data "token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

    The post data gets combined into one so you can also just combine them yourself into a single --data flag
    The post data gets combined into one so you can also just combine them yourself into a single `--data` flag

    curl --data "login=caspyin&token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

  3. Brandon Toone revised this gist Oct 1, 2013. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions curl.md
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,20 @@ Or it can read from STDIN (`@-`)
    }
    end with ctrl+d

    ### Headers ###

    Often when POSTing data you'll need to add headers for things like auth tokens or setting the content type. You can set a header using `-H`.

    curl -H "Content-Type: application/json" -H "authToken: 349ab29a-xtab-423b-a5hc-5623bc39b8c8" --data '{}' https://api.example.com/endpoint


    ### Dealing with HTTPS ###

    If an API doens't have an SSL cert but is using HTTPS you can tell curl to ignore the security by using `--insecure`. Be warned this is a very **"insecure"** thing to do and is only listed here for "educational purposes".

    curl --insecure https://api.example.com/endpoint

    For my own reference mostly, here is where I first learned about using `--insecure` https://github.com/wayneeseguin/rvm/issues/1684

    # OAuth #

  4. @btoone btoone revised this gist Jan 31, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions curl.md
    Original file line number Diff line number Diff line change
    @@ -41,11 +41,11 @@ The post data gets combined into one so you can also just combine them yourself

    curl --data "login=caspyin&token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

    You can tell curl to read from a file (@) to POST data
    You can tell curl to read from a file (`@`) to POST data

    curl --user "caspyin" --data @data.txt https://api.github.com/gists

    Or it can read from STDIN (@-)
    Or it can read from STDIN (`@-`)

    curl --user "caspyin" --data @- https://api.github.com/gists
    {
  5. @btoone btoone created this gist Apr 3, 2012.
    90 changes: 90 additions & 0 deletions curl.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    # Introduction #

    An introduction to curl using GitHub's API

    # The Basics #

    Makes a basic GET request to the specifed URI

    curl https://api.github.com/users/caspyin

    Includes HTTP-Header information in the output

    curl --include https://api.github.com/users/caspyin

    Pass user credential to basic auth to access protected resources like a users starred gists, or private info associated with their profile

    curl --user "caspyin:PASSWD" https://api.github.com/gists/starred
    curl --user "caspyin:PASSWD" https://api.github.com/users/caspyin

    Passing just the username without the colon(:) will cause you to be prompted for your account password. This avoids having your password in your command line history

    curl --user "caspyin" https://api.github.com/users/caspyin

    ## POST ##

    Use the --request (-X) flag along with --data (-d) to POST data

    curl --user "caspyin" --request POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

    curl --user "caspyin" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

    Of course --data implies POST so you don't have to also specify the --request flag

    curl --user "caspyin" --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

    Here is an example that uses the old GitHub API (v2). You can use multiple --data flags

    curl --data "login=caspyin" --data "token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

    The post data gets combined into one so you can also just combine them yourself into a single --data flag

    curl --data "login=caspyin&token=TOKEN" https://github.com/api/v2/json/user/show/caspyin

    You can tell curl to read from a file (@) to POST data

    curl --user "caspyin" --data @data.txt https://api.github.com/gists

    Or it can read from STDIN (@-)

    curl --user "caspyin" --data @- https://api.github.com/gists
    {
    "description":"Test",
    "public":false,
    "files": {
    "file1.txt": {
    "content":"Demo"
    }
    }
    }
    end with ctrl+d


    # OAuth #

    The first thing to know is that your API Token (found in https://github.com/settings/admin) is not the same token used by OAuth. They are different tokens and you will need to generate an OAuth token to be authorized.

    Follow the API's instructions at http://developer.github.com/v3/oauth/ under the sections "Non-Web Application Flow" and "Create a new authorization" to become authorized.

    Note: Use Basic Auth once to create an OAuth2 token http://developer.github.com/v3/oauth/#oauth-authorizations-api

    curl https://api.github.com/authorizations \
    --user "caspyin" \
    --data '{"scopes":["gist"],"note":"Demo"}'

    This will prompt you for your GitHub password and return your OAuth token in the response. It will also create a new Authorized application in your account settings https://github.com/settings/applications

    Now that you have the OAuth token there are two ways to use the token to make requests that require authentication (replace "OAUTH-TOKEN" with your actual token)

    curl https://api.github.com/gists/starred?access_token=OAUTH-TOKEN
    curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/gists/starred

    List the authorizations you already have

    curl --user "caspyin" https://api.github.com/authorizations


    # Resources #

    * HTTParty - Ruby library that makes it easy to create HTTP requests https://github.com/jnunemaker/httparty
    * Hurl IT - An open source web application to play with curl options http://hurl.it