|
From: | Hedin, Richard (InfoSys) |
Subject: | Re: [libmicrohttpd] [EXTERNAL] Re: Can handle Content-Type = application/json ? |
Date: | Fri, 22 Feb 2019 16:13:32 +0000 |
Hi, Justin.
I’ve reproduced the relevant code below. It’s almost exactly the simplepost.c example Christian supplies in his tutorial.
https://www.gnu.org/software/libmicrohttpd/tutorial.html#simplepost_002ec The biggest changes are that 1) I put in debugging writes, 2) I tried to override the Content Type header from application/json to application/x-www-form-urlencoded.
I’ve highlighted the relevant parts in yellow. In my main routine, MHD_start_daemon is passed answer_to_connection as the callback routine. In answer_to_connection
MHD_create_post_processor is called. It returned a null because application/json is not supported.
I’m getting the impression from other posts that I should not use MHD_create_post_processor, but create my own processor of post requests. There should be a
hook that is called when pieces of the request body arrive, but I haven’t found that hook.
Does your JSON-handling code use MHD_create_post_processor?
Regards, Rick static int answer_to_connection (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) { const char* incoming_encoding; // Interesting. Apparently, we point to a
// string that they have allocated and keep ahold of.
LogMessage("In answer_to_connection. Method = \"%s\"", method); if (NULL == *con_cls) { LogMessage("con_cls points to a NULL."); struct connection_info_struct *con_info; con_info = malloc (sizeof (struct connection_info_struct)); if (NULL == con_info) return MHD_NO; con_info->answerstring = NULL; if (0 == strcmp (method, "POST")) { LogMessage("Method is POST."); incoming_encoding = MHD_lookup_connection_value(connection,
MHD_HEADER_KIND,
MHD_HTTP_HEADER_CONTENT_TYPE); LogMessage("Content Type before I changed it was \"%s\"", incoming_encoding); MHD_set_connection_value(connection,
MHD_HEADER_KIND,
MHD_HTTP_HEADER_CONTENT_TYPE,
MHD_HTTP_POST_ENCODING_FORM_URLENCODED); incoming_encoding = NULL; incoming_encoding = MHD_lookup_connection_value(connection,
MHD_HEADER_KIND,
MHD_HTTP_HEADER_CONTENT_TYPE); LogMessage("And after I supposedly changed the content type, the c t it had was \"%s\"", incoming_encoding); con_info->postprocessor =
MHD_create_post_processor (connection, POSTBUFFERSIZE, iterate_post, (void *) con_info); if (NULL == con_info->postprocessor) { LogMessage("con_info->postprocessor is null"); free (con_info); return MHD_NO; } con_info->connectiontype = POST; } else { LogMessage("Method is GET."); con_info->connectiontype = GET; } *con_cls = (void *) con_info; return MHD_YES; } if (0 == strcmp (method, "GET")) { LogMessage("Method is GET."); LogMessage("About to send askpage page."); return send_page (connection, askpage); } if (0 == strcmp (method, "POST")) { LogMessage("Method is POST."); struct connection_info_struct *con_info = *con_cls; if (*upload_data_size != 0) { MHD_post_process (con_info->postprocessor, upload_data, *upload_data_size); *upload_data_size = 0; return MHD_YES; } else if (NULL != con_info->answerstring) { LogMessage("About to send con_info->answerstring page."); return send_page (connection, con_info->answerstring); } } LogMessage("About to send errorpage page."); return send_page (connection, errorpage); } int main(int argc, char** argv) { int portNum; char portStr[10]; char outputDir[80]; char logFile[80]; char logPath[200]; struct MHD_Daemon* d; GetCmdLineStr(argc, argv, "-o", "???", outputDir, sizeof(outputDir) ); GetCmdLineStr(argc, argv, "-l", "???", logFile, sizeof(logFile ) ); if (!strcmp(outputDir, "???") || !strcmp(logFile, "???") ) { printf("Must supply -o outputDir and -l logFile on the command line.\n"); return 1; } GetCmdLineStr(argc, argv, "-p", "???", portStr, sizeof(portStr) ); if (!strcmp(portStr, "???") ) { printf("Must supply -p portStr on the command line.\n"); return 1; } strcpy(logPath, outputDir); strcat(logPath, "/"); strcat(logPath, logFile); portNum = atoi(portStr); SetAppName("webcxn"); SetLogFileName(logPath); SetLoggingOn(TRUE); LogMessage("Webcxn starting"); LogMessage("Webcxn will listen on port number %d", portNum); d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, portNum, NULL, NULL, &answer_to_connection, NULL, MHD_OPTION_NOTIFY_COMPLETED, request_completed, NULL, MHD_OPTION_END); if (d == NULL) { LogMessage("Failed to start the daemon."); return 1; } LogMessage("About to \"pause\" waiting while our daemon does things."); pause(); LogMessage("Came out of the pause. Signal received?"); MHD_stop_daemon(d); return 0; } From: libmicrohttpd <libmicrohttpd-bounces+address@hidden>
On Behalf Of Justin Graves We are running libmicrohttpd in an API which uses JSON in both request and response bodies almost exclusively with no issue. We do also use form data. If the client sends a body
with "application/json" as the "Content-Type" header, we will see whatever they sent via MHD_get_connection_values.
I have just verified this, using libmicrohttp v0.9.62, by sending a request with a "Content-Type" header of "application/json", then using MHD_get_connection_values with MHD_HEADER_KIND,
and it gives me a key of "Content-Type" exactly once with a value of "application/json". Perhaps providing example code related to your header parsing would help? - On Feb 22, 2019, 3:02 AM -0600, Hedin, Richard (InfoSys) <address@hidden>, wrote:
|
[Prev in Thread] | Current Thread | [Next in Thread] |