libevent evhttp如何获得体长或任何身体数据?

时间:2015-05-22 22:30:05

标签: curl libevent

使用下面的简单示例,我试图从生成的帖子的主体中提取数据,如下所示:

curl -v -X POST http://localhost:1067/POST<

我得到的输出如下所述:

$ ./evh len = 0 req =

请帮助!

void process_request(struct evhttp_request *req, void *arg){ 
    struct evbuffer *buf;
    size_t len;
    char *data=malloc(1000);

    if (buf == NULL) return;

    buf=evhttp_request_get_input_buffer(req);

    len=evbuffer_copyout(buf, data, 1000);

    printf("len=%d req=%.*s\n", (int)len, (int)len, data);

    evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));
    evhttp_send_reply(req, HTTP_OK, "OK", buf);
}

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

void process_request(struct evhttp_request* req, void* arg){ 
    struct evbuffer* buf = null;
    size_t len = 0;
    char* data = null;

    if (req == NULL) return; // req is null after a timeout

    // get the event buffer containing POST body
    buf = evhttp_request_get_input_buffer(req);

    // get the length of POST body
    len = evbuffer_get_length(buf);

    // create a char array to extract POST body
    data = malloc(len + 1);
    data[len] = 0;

    // copy POST body into your char array
    evbuffer_copyout(buf, data, len);

    [...]

    // release memory
    free(data);
}