Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling a POST request with libmicrohttpd

Tags:

c++

http

put

I've to use libmicrohttpd to set up a REST server. There is no problem with the GET request, but I don't understand what I'm doing wrong to handle POST (PUT actually) request (JSON format). Here is the code :

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


  // Initializes parser/camera/settings...
  static Parser parser;

  // The first time only the headers are valid, do not respond in the first round
  static int dummy;
  if (*con_cls != &dummy) {
    *con_cls = &dummy;
    return MHD_YES;
  }

  // Parse URL to get the resource
  int resource = parser.getRequestedResource(url);

  // Check wether if it's a GET or a POST method
  if(strcmp(method, MHD_HTTP_METHOD_GET) == 0) {
    parser.processGetRequest(resource);
  }
  else {
    parser.processPutRequest(upload_data, *upload_data_size);
  }

  // Building HTTP response (headers+data)
  MHD_Response* httpResponse = parser.getResponse();

  int ret = MHD_queue_response (connection, MHD_HTTP_OK, httpResponse);

  if (ret != MHD_YES) {
    Logger::get().error("Error queuing message");
  }

  MHD_destroy_response (httpResponse);
  // Clear context pointer
  *con_cls = NULL; 

  return ret;
}

Everytime I try to send a PUT request with some data, I get the "Internal application error, closing connection". The problem may come from one of these things:

  • posting/not-posting response at the first call of the fucntion

  • modifying or not the *upload_data_size (to indicate that processing is done)

  • the good location of *con_cls = NULL instruction

Thanks!

like image 530
Pierre Avatar asked Apr 01 '26 04:04

Pierre


1 Answers

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.

like image 56
David Gloe Avatar answered Apr 02 '26 21:04

David Gloe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!