libmicrohttpd
[Top][All Lists]
Advanced

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

Re: [libmicrohttpd] Multipart Reading


From: silvioprog
Subject: Re: [libmicrohttpd] Multipart Reading
Date: Fri, 9 Nov 2018 14:44:35 -0300

Hi Santos,

it is very easy to handle multi-part and/or form-data in MHD. Some time ago I sent this answer for a StackOverflow user: https://stackoverflow.com/a/32705436/3268398 .

I handle uploads and/or payload (raw content) in the library (built under MHD) I'm maintaining. It allows to get any raw content (JSON, XML etc.) using something like this in the server side:

static void req_cb(__SG_UNUSED void *cls, __SG_UNUSED struct sg_httpreq *req, __SG_UNUSED struct sg_httpres *res) {
struct sg_str *payload = sg_httpreq_payload(req);
sg_httpres_send(res, sg_str_content(payload), "text/plain", 200);
}

and this in the client side (exemplifying in cURL):

curl --header "Content-Type: application/json" --request POST --data '{"abc":123}' -w "\n" http://localhost:<PORT>

the sent raw content will be echoed back to the client. It is easy to iterates uploaded files too:

static void process_uploads(struct sg_httpreq *req, struct sg_httpres *res) {
struct sg_httpupld *upld;
...
upld = sg_httpreq_uploads(req); // first uploaded file
while (upld) {
name = sg_httpupld_name(upld);
if ((errnum = sg_httpupld_save(upld, true)) == 0) // save the file
sg_str_printf(body, "<li><a href="">\"?file=%s\">%s</a></li>", name, name);
else {
sg_strerror(errnum, errmsg, sizeof(errmsg));
sg_str_printf(body, "<li><font color='red'>%s - failed - %s</font></li>", name, errmsg); // handle saving errors
}
sg_httpuplds_next(&upld); // next uploaded file
}
...

Maybe it can be used as a starting point for you to check how to handle the MHD's ... const char *upld_data, size_t *upld_data_size ... AHC parameters. The related lines I've used, are:

https://github.com/risoflora/libsagui/blob/master/src/sg_httpsrv.c#L60

https://github.com/risoflora/libsagui/blob/master/src/sg_httpuplds.c#L119

Hope this helps you. ☺

On Fri, Nov 9, 2018 at 1:04 PM Santos Das <address@hidden> wrote:
Hi,

Can you please let me know how we do read the multupart message body using MHD.

I am having problem in reading the multi part.

Thanks, Santos

--
Silvio Clécio

reply via email to

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