Skip to content

Using JSON

We’ll use cJSON to work with JSON objects in this example, but you can also use jansson if you prefer. For more information, refer to its official documentation.

ecewo install cjson

Copy cJSON.c and cJSON.h files from the repository, and paste them into vendors/ folder. Make sure cJSON.c is part of the CMake build configuration.

Let’s write our hello world example again, but this time it will send a JSON object instead of a plain text.

src/handlers.h
#ifndef HANDLERS_H
#define HANDLERS_H
#include "ecewo.h"
void hello_world(Req *req, Res *res);
#endif
src/handlers.c
#include "handlers.h" // To handle the request and send a response
#include "cJSON.h" // To deal with JSON
void hello_world(Req *req, Res *res)
{
// Create a JSON object
cJSON *json = cJSON_CreateObject();
// Add string to the JSON object we just created
cJSON_AddStringToObject(json, "hello", "world");
// Convert the JSON object to a string
char *json_string = cJSON_PrintUnformatted(json);
// Send the json response with 200 status code
send_json(res, 200, json_string);
// Free the memory that allocated by jansson
cJSON_Delete(json);
free(json_string);
}
src/main.c
#include "server.h"
#include "handlers.h"
void destroy_app(void) {
reset_router();
}
int main(void)
{
init_router();
get("/", hello_world);
shutdown_hook(destroy_app);
ecewo(3000);
return 0;
}

Now we can recompile and send a request to http://localhost:3000/ again. We’ll receive a JSON:

{"hello":"world"}

This time, let’s take a JSON and print it to console.

src/handlers.h
#ifndef HANDLERS_H
#define HANDLERS_H
#include "ecewo.h"
void handle_user(Req *req, Res *res);
#endif
src/handlers.c
#include "handlers.h"
#include "cJSON.h"
void handle_user(Req *req, Res *res)
{
const char *body = req->body;
if (body == NULL)
{
send_text(res, 400, "Missing request body");
return;
}
cJSON *json = cJSON_Parse(body);
if (!json)
{
send_text(res, 400, "Invalid JSON");
return;
}
const char *name = cJSON_GetObjectItem(json, "name")->valuestring;
const char *surname = cJSON_GetObjectItem(json, "surname")->valuestring;
const char *username = cJSON_GetObjectItem(json, "username")->valuestring;
if (!name || !surname || !username)
{
cJSON_Delete(json);
send_text(res, 400, "Missing fields");
return;
}
printf("Name: %s\n", name);
printf("Surname: %s\n", surname);
printf("Username: %s\n", username);
cJSON_Delete(json);
send_text(res, 200, "Success!");
}
src/main.c
#include "server.h"
#include "handlers.h"
void destroy_app(void) {
reset_router();
}
int main(void)
{
init_router();
post("/user", handle_user);
shutdown_hook(destroy_app);
ecewo(3000);
return 0;
}

Let’s recompile the program and send a POST request to http://localhost:3000/user with this body:

{
"name": "John",
"surname": "Doe",
"username": "johndoe"
}

We’ll see in the console:

Name: John
Surname: Doe
Username: johndoe