[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] POST data processing

From: silvioprog
Subject: Re: [libmicrohttpd] POST data processing
Date: Thu, 17 May 2018 10:26:42 -0300

Hello Austin,

As Christian explained, via "upload_data" you can receive any payload kind. I'm writting a library which maps MHD callbacks to "objects" and it needs to support the following features:

1. receive one or more files on demand (to receive large files about 5 GB+ without "frying" CPU/RAM) in same request (files via form-data);
2. receive form fields (HTML-form fields via x-www-form-urlencoded);
3. 1 and 2 (both files and fields via form-data);
4. payload contents (raw-data, json/xml and so on via application/<type>);
5. handlers to create customized 'body-parser' if none of the above options fits.

it seems the option 4 above is the one you are looking for. For example, supposing you need to send a JSON "{"foo":"bar"}" to the server, in the client:

curl -X POST -H "Content-Type: application/json" -d '{"foo":"bar"}' http://localhost:8080

and in a minimal snipped server example, you can receive it as:


static void req_cb(void *cls, struct bk_httpreq *req, struct bk_httpres *res) {
    struct bk_str *payload = bk_httpreq_payload(req);
    printf("Payload string: %s; Size: %zd", bk_str_content(payload), bk_str_length(payload));

int main(void) {
    struct bk_httpsrv *srv = bk_httpsrv_new(req_cb, NULL);
    bk_httpsrv_listen(srv, 8080, false);


then it prints "Payload string: {"foo":"bar"}; Size: 13" in the terminal. The "payload" variable above is an instance of the object "bk_str" which contains useful "methods" for string handling like "content", "length", "clear", "printf" and more.

If you want to take a look how it was implemented, please use the branch "new_api" and specifically the line 78 from bk_httpuplds.c file:

hope this help you.

(the library is under development, so there is parts undocumented yet but feel free to send any questions via Github issues)

On Thu, May 17, 2018 at 1:52 AM, Austin Einter <address@hidden> wrote:
I am using libmicrohttpd and facing issue with POST processing.

The POST message received contains xml or json data. libmicrohttpd does not support xml / json. So I can not use post process or iterate callbacks for body data processing. In fact I tried and it does not work (iterate callbacks not called even when I call post process).

So I am left with two options.

1) Either write body parser, where I need to take care of content-length case, chunked data case etc. 

2) OR use http-parser kind of open source

Is there any other option available?

I am not very pro to write my own parser, instead I would like to use http-parser. I just looked at http-parser. To use http-parser, we need to provide the entire message to http-parser, either single time or one chunk at a time.

Is there anyway in libmicrohttpd, I can collect the entire POST message as it comes / received at socket level?

If possible, kindly provide required steps / info.

Best Regards
Silvio Clécio

reply via email to

[Prev in Thread] Current Thread [Next in Thread]