#include #include #include #include #include #include #include #include // cjson 用于解析 json int main(int argc, char const *argv[]) { // 获取上传的数据 char request_data[128] = ""; fgets(request_data, 128, stdin); // int fd = open("tmp.txt", O_RDWR | O_CREAT, 0666); // write(fd, request_data, strlen(request_data)); // 要求:请求上传的数据类型必须是 json 格式的字符串 cJSON *p = cJSON_Parse(request_data); cJSON *name = cJSON_GetObjectItemCaseSensitive(p, "name"); cJSON *pwd = cJSON_GetObjectItemCaseSensitive(p, "pwd"); printf("content-type: application/json;charset=utf-8\r\n"); printf("\r\n"); if (strncmp(name->valuestring, "disen", 5) == 0 && strncmp(pwd->valuestring, "123123", 6) == 0) { printf("{\"code\":0,\"data\":{\"user_id\":111,\"nickName\":\"老狄\"}}"); } else { printf("{\"code\":1,\"msg\":\"用户名或口令错误\"}"); } // write(fd, name->valuestring, strlen(name->valuestring)); // write(fd, pwd->valuestring, strlen(pwd->valuestring)); // close(fd); // json 对象中包含一个 data 对象 return 0; }