1
我必須使用libmicrohttpd來設置REST服務器。 GET請求沒有問題,但我不明白我在做什麼錯誤來處理POST(實際上是PUT)請求(JSON格式)。下面是代碼:使用libmicrohttpd處理POST請求
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;
}
每次我試圖發送一個PUT請求一些數據,我得到的「內部應用程序錯誤,關閉連接」。這個問題可能來自這些事情之一:在溫控功能的第一個電話
修改與否* upload_data_size
發佈/不發佈響應(以指示處理完成)
*con_cls = NULL
指令
感謝的好位置!
太晚了,但這就是我需要:) – Pierre