Print an error on invalid request JSON

When a request to the REST API is made with invalid JSON, it's hard to see
why the request fails due to the fact that no error is sent.
This commit is contained in:
Markus Mäkelä
2018-07-18 09:29:12 +03:00
parent 609a6723a8
commit 6b8d9dc5d9

View File

@ -106,8 +106,10 @@ int Client::process(string url, string method, const char* upload_data, size_t *
if (m_data.length() &&
(json = json_loadb(m_data.c_str(), m_data.size(), 0, &err)) == NULL)
{
MHD_Response *response =
MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
string msg = string("{\"errors\": [ { \"detail\": \"Invalid JSON in request: ")
+ err.text + "\" } ] }";
MHD_Response *response = MHD_create_response_from_buffer(msg.size(), &msg[0],
MHD_RESPMEM_MUST_COPY);
MHD_queue_response(m_connection, MHD_HTTP_BAD_REQUEST, response);
MHD_destroy_response(response);
return MHD_YES;