直接上代码。
mian.cpp
#include <event.h>
//for http
#include <evhttp.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/http_compat.h>
#include <event2/util.h>
#include "cJSON.h"
#include <unistd.h>
#include <iostream>
#include <cstring>
struct evhttp *m_phttpd;
void login_handler(struct evhttp_request *req, void *arg)
{
//获取POST方法的数据
size_t post_size = EVBUFFER_LENGTH(req->input_buffer);
char request_data[1024] = { 0 };
char *post_data = (char *)EVBUFFER_DATA(req->input_buffer);
cJSON *root = NULL;
if (post_size > 0 && post_size < 1024)
{
memcpy(request_data, post_data, post_size);
root = cJSON_Parse(request_data);
}
//给前段回复一个响应结果
cJSON*response_root = cJSON_CreateObject();
if (root && response_root)
{
cJSON* username_obj = cJSON_G