libmicrohttpd
[Top][All Lists]
Advanced

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

[libmicrohttpd] Create a server that returns string sent in request payl


From: Dawid Czeluśniak
Subject: [libmicrohttpd] Create a server that returns string sent in request payload
Date: Mon, 3 Jun 2019 20:47:10 +0200

Hi!

I would like to create a simple webserver that returns the exact payload that was sent with request. Here is the code that I wrote:

"""
#include <stdio.h>
#include <string.h>
#include <microhttpd.h>

#define PORT 8080

int on_connection(void *cls, struct MHD_Connection *conn, const char *url,
                  const char *method, const char *version, const char *upload_data,
                  size_t *upload_data_size, void **conn_cls) {
 
    if (strcmp(method, "POST") == 0) {
        if (*upload_data_size == 0) {
            return MHD_YES;
        } else {
            *upload_data_size = 0;
        }
    }

    puts(upload_data);
    puts(method);

    struct MHD_Response *res = MHD_create_response_from_buffer(strlen(upload_data),
                                                               (void *)upload_data,
                                                               MHD_RESPMEM_PERSISTENT);
    return MHD_queue_response(conn, MHD_HTTP_OK, res);
}

int main() {
    struct MHD_Daemon *daemon;
    daemon = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,
                              &on_connection, NULL,
                              MHD_OPTION_END);
    getchar();
    MHD_stop_daemon(daemon);
    return 0;
}
"""

But the problem with this code is that it does not return any response. When I try to send a request with curl:
curl -X POST localhost:8080 -d "Hello"

I see in that server itself prints:

Hello
POST

every time I make a request, but curl command returns:
curl: (52) Empty reply from server

I have already noticed that when I sent POST request on_connection function is called twice: first time with *upload_data_size = 0 and the second time with *upload_data_size equals the size of the payload.

Could you help me with that?
Thanks,
czelusniakdawid

reply via email to

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