This was a very educational guided-project experience in writing HTTP API endpoints in Go. It taught me a lot about how data is communicated between a client and a server using HTTP protocols.
Request Body:
{
"email": "[email protected]",
"password": "notasecurepassword123"
}
Response Body:
{
"id": 1,
"email": "[email protected]"
}
Headers needed:
Authorization: Bearer <token>
Request Body:
{
"email": "[email protected]",
"password": "atotallysecurepassword389"
}
Response Body:
{
"id": 1,
"email": "[email protected]"
}
Request Body:
{
"email": "[email protected]",
"password": "atotallysecurepassword389"
}
Response Body:
{
"id": 1,
"email": "[email protected]",
"is_chirpy_red": false,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjaGlycHktYWNjZXNzIiwic3ViIjoiMSIsImV4cCI6MTY4NTIyNDQ4MiwiaWF0IjoxNjg1MjIwODgyfQ.zvUD8YgYbp17RUnbuMydhyBlTcuAQcL4Gt4vkDSqCOE",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjaGlycHktcmVmcmVzaCIsInN1YiI6IjEiLCJleHAiOjE2OTA0MDQ4ODIsImlhdCI6MTY4NTIyMDg4Mn0.yfaZs2dhNj9AQxkJbWoCK2NuLsfjWZTEXBaC-4QFkiY"
}
Headers required:
Authorization: ApiKey <key>
Request Body:
{
"event": "user.upgraded",
"data": {
"user_id": 1
}
}
Response Body:
null
Response Code: 200
If event
is anything other than user.upgraded
, will not upgrade user and return 200
. If request does not provide or has an incorrect polka apikey, response code will be 401
. If response contains user.upgraded
and has a valid polka apikey, will return code 200
.
Headers Required:
Authorization: Bearer <refresh-token>
No Request Body expected
Response Body:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjaGlycHktYWNjZXNzIiwic3ViIjoiMSIsImV4cCI6MTY4NTIyNDQ4MiwiaWF0IjoxNjg1MjIwODgyfQ.zvUD8YgYbp17RUnbuMydhyBlTcuAQcL4Gt4vkDSqCOE"
}
Headers Required:
Authorization: Bearer <refresh-token>
No Request Body expected
Response Body:
null
Response Code: 200
If response code is not 200
, then you will get an error and a corresponding code instead.
Chirps can only be created by Users that have been created and logged in (requires access token). Chirps' contents must be 140 characters or less. If they contain the words ["kerfuffle", "sharbert", "fornax"]
they will be censored with ****
.
Headers Required:
Authorization: Bearer <token>
Request Body:
{
"body": "this is an example chirp~"
}
Response Body:
{
"id": 1,
"body": "this is an example chirp~",
"author_id": 1
}
id
is the chirp id and author_id
is the id of the corresponding user who made the chirp.
Optional query parameters (in url)
author
parameter (e.g.GET http://localhost:8080/api/chirps?author_id=1
) and if present, returns all the chirps by the specified author. If author user doesn't exist, returns an empty list.sort
parameter (e.g.GET http://localhost:8080/api/chirps?sort=desc
), default isasc
ascending order, can only specify eitherdesc
descending orasc
ascending- of course, can provide both at the same time like
GET http://localhost:8080/api/chirps?sort=asc&author_id=2
- of course, can provide both at the same time like
Response Body:
[
{
"id": 1,
"body": "this is my first chirp!!",
"author_id": 1
},
{
"id": 2,
"body": "this is my second **** chirp!",
"author_id": 1
}
]
Chirps are ordered by id
in ascending order.
Example request: GET localhost:8080/api/chirps/2
Response Body:
{
"id": 2,
"body": "this is my second **** chirp!",
"author_id": 1
}
You must provide an access token and you can only delete chirps that you have created.
Example request: DELETE localhost:8080/api/chirps/2
Headers Required:
Authorization: Bearer <token>
No Request Body expected.
Response Body:
null
Response Code: 200
As always, if there is some error, you will be given an appropriate response code and error message in the body.
Response Body:
OK
Response Code: 200
If receive anything else, server is not up.
Response Body:
<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited 1 times!</p>
</body>
</html>
The template of the html page can be changed in the file /admin/metrics/template.html
Response Body:
<html>
<body>
<h1>Welcome to Chirpy</h1>
</body>
</html>
The template of the html page can be changed in the file /index.html
You need a .env
that contains
JWT_SECRET=<your-super-secret-at-least-32-char-long-key>
POLKA_KEY=<super-secret-api-key>
Notes:
- "Chirpy Red" is a fictitious elevated subscription tier that users get upgraded to from
- Project written following the outlines of "Learn Web Servers" on boot.dev