From fccb427c88e6b6e0ae8627ffcdc06f4ae5077d34 Mon Sep 17 00:00:00 2001 From: "David M. Lee" Date: Wed, 27 Nov 2013 15:48:39 +0000 Subject: ari:Add application/json parameter support The patch allows ARI to parse request parameters from an incoming JSON request body, instead of requiring the request to come in as query parameters (which is just weird for POST and DELETE) or form parameters (which is okay, but a bit asymmetric given that all of our responses are JSON). For any operation that does _not_ have a parameter defined of type body (i.e. "paramType": "body" in the API declaration), if a request provides a request body with a Content type of "application/json", the provided JSON document is parsed and searched for parameters. The expected fields in the provided JSON document should match the query parameters defined for the operation. If the parameter has 'allowMultiple' set, then the field in the JSON document may optionally be an array of values. (closes issue ASTERISK-22685) Review: https://reviewboard.asterisk.org/r/2994/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@403177 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- rest-api-templates/param_parsing.mustache | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'rest-api-templates/param_parsing.mustache') diff --git a/rest-api-templates/param_parsing.mustache b/rest-api-templates/param_parsing.mustache index aabd728fd..9d2073869 100644 --- a/rest-api-templates/param_parsing.mustache +++ b/rest-api-templates/param_parsing.mustache @@ -83,3 +83,66 @@ {} } {{/has_path_parameters}} +{{^is_websocket}} +{{#parse_body}} + /* Look for a JSON request entity */ + body = ast_http_get_json(ser, headers); + if (!body) { + switch (errno) { + case EFBIG: + ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large"); + goto fin; + case ENOMEM: + ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request"); + goto fin; + case EIO: + ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body"); + goto fin; + } + } +{{#body_parameter}} + args.{{c_name}} = ast_json_ref(body); +{{/body_parameter}} +{{^body_parameter}} + /* Parse query parameters out of it */ +{{#query_parameters}} + field = ast_json_object_get(body, "{{name}}"); + if (field) { +{{^allow_multiple}} + args.{{c_name}} = {{json_convert}}(field); +{{/allow_multiple}} +{{#allow_multiple}} + /* If they were silly enough to both pass in a query param and a + * JSON body, free up the query value. + */ + ast_free(args.{{c_name}}); + if (ast_json_typeof(field) == AST_JSON_ARRAY) { + /* Multiple param passed as array */ + size_t i; + args.{{c_name}}_count = ast_json_array_size(field); + args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count); + + if (!args.{{c_name}}) { + ast_ari_response_alloc_failed(response); + goto fin; + } + + for (i = 0; i < args.{{c_name}}_count; ++i) { + args.{{c_name}}[i] = {{json_convert}}(ast_json_array_get(field, i)); + } + } else { + /* Multiple param passed as single value */ + args.{{c_name}}_count = 1; + args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count); + if (!args.{{c_name}}) { + ast_ari_response_alloc_failed(response); + goto fin; + } + args.{{c_name}}[0] = {{json_convert}}(field); + } +{{/allow_multiple}} + } +{{/query_parameters}} +{{/body_parameter}} +{{/parse_body}} +{{/is_websocket}} -- cgit v1.2.3