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!