Skip to content

naiih001/cpp-notes-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Notes API

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.

Prerequisites

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/json library (must be findable by CMake)
  • fmt library (must be findable by CMake)

Building the Project

  1. Clone the repository:

    git clone <repository-url>
    cd notes-api
  2. Create a build directory:

    mkdir build
    cd build
  3. Configure the project with CMake:

    cmake ..
  4. Compile the project:

    make

    This will create an executable named notes in the build directory.

Running the Server

To start the API server, run the executable from the project's root or build directory:

./build/notes

The server will start and listen on port 8080.

API Endpoints

The API provides the following endpoints for managing notes:

1. Get All 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."
          }
      ]

2. Create a New Note

  • Endpoint: POST /notes
  • Description: Creates a new note.
  • Request Body: A JSON object containing the title and content of the note.
    {
        "title": "New Note Title",
        "content": "Content of the new note."
    }
  • Success Response:
    • Code: 201 Created
    • Content: A JSON object with the id of the newly created note.
      {
          "id": 3
      }
  • Error Response:
    • Code: 400 Bad Request if the request body is not valid JSON.

3. Get a Specific Note

  • 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."
      }
  • Error Response:
    • Code: 404 Not Found if a note with the specified ID does not exist.

4. Update a Note

  • Endpoint: PUT /notes/{id}
  • Description: Updates the title and content of an existing note.
  • Request Body: A JSON object with the new title and content.
    {
        "title": "Updated Title",
        "content": "Updated content."
    }
  • Success Response:
    • Code: 200 OK
  • Error Response:
    • Code: 404 Not Found if the note with the specified ID does not exist.
    • Code: 400 Bad Request if the request body is not valid JSON.

5. Delete a Note

  • Endpoint: DELETE /notes/{id}
  • Description: Deletes a note by its ID.
  • Success Response:
    • Code: 200 OK
  • Error Response:
    • Code: 404 Not Found if the note with the specified ID does not exist.

About

Created a note-taking API using cpp-httplib.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors