[mod_verto] Support for JSON message body in the HTTP POST requst

This commit is contained in:
Ajay Sabat 2023-10-11 19:13:17 -07:00
parent 44e228b31d
commit 4815f45767
1 changed files with 68 additions and 71 deletions

View File

@ -1765,7 +1765,13 @@ new_req:
} }
if (!strncmp(request->method, "POST", 4) && request->content_length && request->content_type) { if (!strncmp(request->method, "POST", 4) && request->content_length && request->content_type) {
switch_bool_t content_type_urlencoded = SWITCH_FALSE;
switch_bool_t content_type_json = SWITCH_FALSE;
content_type_urlencoded = !strncmp(request->content_type, "application/x-www-form-urlencoded", 33);
content_type_json = !strncmp(request->content_type, "application/json", 16);
if (content_type_urlencoded || content_type_json) {
char *buffer = NULL; char *buffer = NULL;
switch_ssize_t len = 0, bytes = 0; switch_ssize_t len = 0, bytes = 0;
@ -1796,24 +1802,15 @@ new_req:
*(buffer + bytes) = '\0'; *(buffer + bytes) = '\0';
// Supports both urlencoded and json message body in the http request if (content_type_urlencoded) {
if (!strncmp(request->content_type, "application/x-www-form-urlencoded", 33)) {
kws_parse_qs(request, buffer); kws_parse_qs(request, buffer);
} else if (!strncmp(request->content_type, "application/json", 16)) { } else if (content_type_json) {
cJSON *json = NULL;
json = cJSON_Parse(buffer);
if (json) {
switch_event_set_body(stream.param_event, buffer); switch_event_set_body(stream.param_event, buffer);
cJSON_Delete(json);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid JSON data in message body. content_length: %ld, body: %s \n", request->content_length, buffer);
free(buffer);
goto request_err;
}
} }
free(buffer); free(buffer);
} }
}
// kws_request_dump(request); // kws_request_dump(request);