最近有项目后端需要使用http协议,使用restful的接口方式给前端界面使用,找了很多开源库,不是需要C++11的支持,就是需要依赖boost库,要么只支持Linux。
找了很久并进行比较后,选取了libmicrohttpd作为底层http的通信库,测试完成后发现post请求的Content-Type只支持multipart/form-data和application/x-www-form-urlencoded,不支持application/json。让我十分头疼。经过摸索发现了一种可以获取application/json的data的方法。如以下代码所示。
/* Feel free to use this example code in any way
you see fit (Public Domain) */
#include <sys/types.h>
#ifndef _WIN32
#include <sys/select.h>
#include <sys/socket.h>
#else
#include <winsock2.h>
#endif
#include <microhttpd/microhttpd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if defined(_MSC_VER) && _MSC_VER + 0 <= 1800
/* Substitution is OK while return value is not used */
#define snprintf _snprintf
#endif
#define MHD_HTTP_POST_ENCODING_APP_JSON "application/json"
#define PORT 8888
#define POSTBUFFERSIZE 512
#defi

886

被折叠的 条评论
为什么被折叠?



