libmicrohttpd
[Top][All Lists]
Advanced

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

[libmicrohttpd] Access request body for form post and json parsing


From: Nicolas Mora
Subject: [libmicrohttpd] Access request body for form post and json parsing
Date: Mon, 26 Oct 2015 14:11:19 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0

Hello,

I'm currently trying to develop a web framework based on MHD. While trying to access request body, I have some difficulties to handle it.

There are 2 different access types: form-based HTTP POST and JSON based HTTP REST calls.

For the first type, if I want to access its value, I can't do it with MHD_get_connection_values for a reason I don't undserstand yet.

If I use MHD_create_post_processor on a well-formed HTTP form-based POST, I'm able to parse the POST parameters but in that case, the HTTP call must be with a specific content-type.

I tried to use upload_data to get raw request body data, but it seems not to be filled when I need it.

Here is my the part of my code where I have this problem:

/**
 * Initialize the framework environment and start the instance
 */
int ulfius_init_framework(struct _u_instance * u_instance, struct _u_endpoint * endpoint_list) {
        u_instance->mhd_daemon = MHD_start_daemon 
(MHD_USE_THREAD_PER_CONNECTION,
u_instance->port, NULL, NULL, &ulfius_webservice_dispatcher, (void *)endpoint_list,
        MHD_OPTION_NOTIFY_COMPLETED, request_completed, NULL,
        MHD_OPTION_SOCK_ADDR, u_instance->bind_address,
        MHD_OPTION_END);
        
        return 0;
}

/**
 * Redirect the http call to the proper function
 */
int ulfius_webservice_dispatcher (void *cls, struct MHD_Connection *connection,
                                const char *url, const char *method,
const char *version, const char *upload_data,
                                size_t *upload_data_size, void **con_cls) {
struct _u_endpoint * endpoint_list = (struct _u_endpoint *)cls, * current_endpoint; struct _u_map * url_map = malloc(sizeof(struct _u_map)), * header_map = malloc(sizeof(struct _u_map)), * cookie_map = malloc(sizeof(struct _u_map)), * post_map = malloc(sizeof(struct _u_map));
        struct connection_info_struct * con_info_post = NULL;
        struct connection_info_struct * con_info = *con_cls;
        u_map_init(url_map);
        u_map_init(header_map);
        u_map_init(cookie_map);
        u_map_init(post_map);
        
        current_endpoint = endpoint_match(method, url, endpoint_list);
        
printf("Endpoint match: %s\n", current_endpoint!=NULL?current_endpoint->url_format:"none");
        
        parse_url(url, current_endpoint->url_format, url_map);
MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, ulfius_fill_map, url_map); MHD_get_connection_values (connection, MHD_HEADER_KIND, ulfius_fill_map, header_map); MHD_get_connection_values (connection, MHD_COOKIE_KIND, ulfius_fill_map, cookie_map); MHD_get_connection_values (connection, MHD_POST_KIND, ulfius_fill_map, post_map);
        
        
        if (current_endpoint != NULL) {
        }
        printf("print url\n");
        print_map(url_map);
        printf("print header\n");
        print_map(header_map);
        printf("print cookie\n");
        print_map(cookie_map);
        printf("print post data\n");
        print_map(post_map);
        printf("\n");
        return 0;
}

Basically, I would like to know if there's a way to use MHD_get_connection_values (connection, MHD_POST_KIND, ulfius_fill_map, post_map); all the time, and also a way to get raw request body to parse it in a json format ?

Thanks

/Nicolas



reply via email to

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