Handling a POST request with libmicrohttpd

无人久伴 提交于 2019-12-06 05:35:41

I ran into this same problem and discovered the solution through some debugging.

When the library calls your handler with request_data, you're not allowed to queue any responses (MHD_queue_response returns MHD_NO). You need to wait until the final handler call with no request_data to call MHD_queue_response.

This behavior isn't documented as far as I can tell.

I'm using GNU libmicrohttpd too, and I found a simple POST demo on its repository.

The demo is a little bit simple: it has a form that asks about your name, so when you type your name and click in the "Send" button, the posted data is processed in the answer_to_connection() function:

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)
{


...

  if (0 == strcmp (method, "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)
        return send_page (connection, con_info->answerstring);
    }
...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!