kristapsdz / kcgi

minimal CGI and FastCGI library for C/C++
https://kristaps.bsd.lv/kcgi
ISC License
275 stars 40 forks source link

How to read the raw request body for POST/PUT requests #84

Closed Davester47 closed 3 years ago

Davester47 commented 4 years ago

This is a very well written and easy to use library. I was just wondering whether it is possible to read the body of a request and not just key/value pairs using kreq.fields. Is there something similar to khttp_write() but for reading instead? I'm looking for something like khttp_read(). I'm trying to implement a REST api, and I need this feature. I couldn't find anything like it in the documentation.

I noticed the khttp_parse.3 manpage mentioned that khttp_parse and khttp_parsex have the capability to read and validate key-value form data and opaque message bodies, but I didn't see anywhere that said how to access an opaque message body. Would an operation like this violate the sandboxing requirements?

RoboSparrow commented 3 years ago

anonymous data is stored in req.fields[0]

curl -d "[1,2,3]" -H "Content-Type: application/json" -X POST http://localhost:3000/data
if (req.fieldsz) {
   printf("raw: '%s'\n", req.fields[0].val);
   printf("sanitized: '%s'\n", req.fields[0].parsed.s);
}

// raw: '[1,2,3]'
// sanitized: '[1,2,3]'
Davester47 commented 3 years ago

Thank you.