Skip to content

Commit 8b46d5c

Browse files
committed
docs: rest api basics
1 parent 2ee55a5 commit 8b46d5c

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/REST_BASICS.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# REST API Basics
2+
3+
REST (Representational State Transfer) is a standard for building web APIs using HTTP. Below are the core concepts with practical `curl` examples, using the OpenAI API as a reference.
4+
5+
## HTTP Verbs
6+
7+
- **GET**: Retrieve data.
8+
9+
```bash
10+
curl https://api.openai.com/v1/models \
11+
-H "Authorization: Bearer $OPENAI_API_KEY"
12+
```
13+
14+
- **POST**: Create new resources.
15+
16+
```bash
17+
curl https://api.openai.com/v1/chat/completions \
18+
-H "Authorization: Bearer $OPENAI_API_KEY" \
19+
-H "Content-Type: application/json" \
20+
-d '{"model":"gpt-4","messages":[{"role":"user","content":"Hello"}]}'
21+
```
22+
23+
- **PUT**: Replace a resource.
24+
- **PATCH**: Update part of a resource.
25+
- **DELETE**: Remove a resource.
26+
27+
## Headers
28+
29+
- **Authorization**: Used for authentication.
30+
31+
```bash
32+
-H "Authorization: Bearer $OPENAI_API_KEY"
33+
```
34+
35+
- **Content-Type**: Specifies the body format.
36+
- `application/json` for JSON data
37+
- `application/x-www-form-urlencoded` for form data
38+
- `multipart/form-data` for file uploads
39+
40+
## Body
41+
42+
- **JSON**: Most APIs accept JSON.
43+
44+
```bash
45+
-d '{"key":"value"}'
46+
```
47+
48+
- **Form Data**:
49+
50+
```bash
51+
-d "key1=value1&key2=value2" \
52+
-H "Content-Type: application/x-www-form-urlencoded"
53+
```
54+
55+
- **Binary/File Upload**:
56+
57+
```bash
58+
curl -F "file=@myfile.png" https://api.example.com/upload
59+
```
60+
61+
## Parameters
62+
63+
- **Query Parameters**: Appended to the URL.
64+
65+
```bash
66+
curl "https://api.example.com/data?utm_source=github&utm_medium=readme"
67+
```
68+
69+
- **Common Examples**:
70+
- `utm_source`, `utm_medium`, `utm_campaign` for analytics
71+
- `limit`, `offset` for pagination
72+
73+
---
74+
75+
**Pro Tip:** Always check the API documentation for required headers, body formats, and supported parameters.

0 commit comments

Comments
 (0)