Skip to content

Commit 300db38

Browse files
committed
modified curl.md
1 parent 5cca639 commit 300db38

File tree

1 file changed

+99
-6
lines changed

1 file changed

+99
-6
lines changed

curl.md

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,104 @@
1-
# `curl`
1+
# Introduction #
22

3-
## Getting raw file from GitHub
3+
An introduction to curl using GitHub's API
44

5-
```shell
6-
curl -sL 'https://github.com/path/to/raw/file.md'
7-
```
5+
# The Basics #
86

9-
---
7+
Makes a basic GET request to the specifed URI
108

9+
curl https://api.github.com/users/caspyin
1110

11+
Includes HTTP-Header information in the output
12+
13+
curl --include https://api.github.com/users/caspyin
14+
15+
Pass user credential to basic auth to access protected resources like a users starred gists, or private info associated with their profile
16+
17+
curl --user "caspyin:PASSWD" https://api.github.com/gists/starred
18+
curl --user "caspyin:PASSWD" https://api.github.com/users/caspyin
19+
20+
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
21+
22+
curl --user "caspyin" https://api.github.com/users/caspyin
23+
24+
## POST ##
25+
26+
Use the `--request` (`-X`) flag along with `--data` (`-d`) to POST data
27+
28+
curl --user "caspyin" --request POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists
29+
30+
curl --user "caspyin" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists
31+
32+
Of course `--data` implies POST so you don't have to also specify the `--request` flag
33+
34+
curl --user "caspyin" --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists
35+
36+
Here is an example that uses the old GitHub API (v2). You can use multiple `--data` flags
37+
38+
curl --data "login=caspyin" --data "token=TOKEN" https://github.com/api/v2/json/user/show/caspyin
39+
40+
The post data gets combined into one so you can also just combine them yourself into a single `--data` flag
41+
42+
curl --data "login=caspyin&token=TOKEN" https://github.com/api/v2/json/user/show/caspyin
43+
44+
You can tell curl to read from a file (`@`) to POST data
45+
46+
curl --user "caspyin" --data @data.txt https://api.github.com/gists
47+
48+
Or it can read from STDIN (`@-`)
49+
50+
curl --user "caspyin" --data @- https://api.github.com/gists
51+
{
52+
"description":"Test",
53+
"public":false,
54+
"files": {
55+
"file1.txt": {
56+
"content":"Demo"
57+
}
58+
}
59+
}
60+
end with ctrl+d
61+
62+
### Headers ###
63+
64+
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`.
65+
66+
curl -H "Content-Type: application/json" -H "authToken: 349ab29a-xtab-423b-a5hc-5623bc39b8c8" --data '{}' https://api.example.com/endpoint
67+
68+
69+
### Dealing with HTTPS ###
70+
71+
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".
72+
73+
curl --insecure https://api.example.com/endpoint
74+
75+
For my own reference mostly, here is where I first learned about using `--insecure` https://github.com/wayneeseguin/rvm/issues/1684
76+
77+
# OAuth #
78+
79+
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.
80+
81+
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.
82+
83+
Note: Use Basic Auth once to create an OAuth2 token http://developer.github.com/v3/oauth/#oauth-authorizations-api
84+
85+
curl https://api.github.com/authorizations \
86+
--user "caspyin" \
87+
--data '{"scopes":["gist"],"note":"Demo"}'
88+
89+
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
90+
91+
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)
92+
93+
curl https://api.github.com/gists/starred?access_token=OAUTH-TOKEN
94+
curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/gists/starred
95+
96+
List the authorizations you already have
97+
98+
curl --user "caspyin" https://api.github.com/authorizations
99+
100+
101+
# Resources #
102+
103+
* HTTParty - Ruby library that makes it easy to create HTTP requests https://github.com/jnunemaker/httparty
104+
* Hurl IT - An open source web application to play with curl options http://hurl.it

0 commit comments

Comments
 (0)