A simple C++ RESTful API for creating, reading, updating, and deleting notes.
This project provides a basic note-taking service with a JSON-based API. It's built using C++ and the httplib.h library for the server, and nlohmann/json for JSON manipulation.
Before you begin, ensure you have the following installed:
- A C++17 compatible compiler (like GCC or Clang)
- CMake (version 3.10 or higher)
nlohmann/jsonlibrary (must be findable by CMake)fmtlibrary (must be findable by CMake)
-
Clone the repository:
git clone <repository-url> cd notes-api
-
Create a build directory:
mkdir build cd build -
Configure the project with CMake:
cmake ..
-
Compile the project:
make
This will create an executable named
notesin thebuilddirectory.
To start the API server, run the executable from the project's root or build directory:
./build/notesThe server will start and listen on port 8080.
The API provides the following endpoints for managing notes:
- Endpoint:
GET /notes - Description: Retrieves a list of all notes.
- Success Response:
- Code:
200 OK - Content: An array of note objects.
[ { "id": 1, "title": "My First Note", "content": "This is the content of my first note." }, { "id": 2, "title": "Another Note", "content": "Some more text." } ]
- Code:
- Endpoint:
POST /notes - Description: Creates a new note.
- Request Body: A JSON object containing the
titleandcontentof the note.{ "title": "New Note Title", "content": "Content of the new note." } - Success Response:
- Code:
201 Created - Content: A JSON object with the
idof the newly created note.{ "id": 3 }
- Code:
- Error Response:
- Code:
400 Bad Requestif the request body is not valid JSON.
- Code:
- Endpoint:
GET /notes/{id} - Description: Retrieves a single note by its ID.
- Success Response:
- Code:
200 OK - Content: The note object.
{ "id": 1, "title": "My First Note", "content": "This is the content of my first note." }
- Code:
- Error Response:
- Code:
404 Not Foundif a note with the specified ID does not exist.
- Code:
- Endpoint:
PUT /notes/{id} - Description: Updates the
titleandcontentof an existing note. - Request Body: A JSON object with the new
titleandcontent.{ "title": "Updated Title", "content": "Updated content." } - Success Response:
- Code:
200 OK
- Code:
- Error Response:
- Code:
404 Not Foundif the note with the specified ID does not exist. - Code:
400 Bad Requestif the request body is not valid JSON.
- Code:
- Endpoint:
DELETE /notes/{id} - Description: Deletes a note by its ID.
- Success Response:
- Code:
200 OK
- Code:
- Error Response:
- Code:
404 Not Foundif the note with the specified ID does not exist.
- Code: