libevent evhttp 如何获取主体长度或任何主体数据?
libevent evhttp how do I get the body length or indeed any body data?
使用下面的简单示例,我试图从像这样生成的 post 的正文中提取数据:
curl -v -X POST http://localhost:1067/POST <
我得到的输出如下所述:
$./evh
len=0 请求=
请帮助!
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);
}
希望这会有所帮助:
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);
}
使用下面的简单示例,我试图从像这样生成的 post 的正文中提取数据:
curl -v -X POST http://localhost:1067/POST <
我得到的输出如下所述:
$./evh len=0 请求=
请帮助!
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);
}
希望这会有所帮助:
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);
}