diff --git a/include/spdk/jsonrpc.h b/include/spdk/jsonrpc.h index a29814957..ff9438a7d 100644 --- a/include/spdk/jsonrpc.h +++ b/include/spdk/jsonrpc.h @@ -182,16 +182,15 @@ void spdk_jsonrpc_send_error_response_fmt(struct spdk_jsonrpc_request *request, * on the request after writing the desired request object to the spdk_json_write_ctx. * * \param request JSON-RPC request. - * \param method Name of the RPC method. - * \param id ID index for the request. + * \param id ID index for the request. If < 0 skip ID. + * \param method Name of the RPC method. If NULL caller will have to create "method" key. * * \return JSON write context to write the parameter object to, or NULL if no * parameter is necessary. */ -struct spdk_json_write_ctx *spdk_jsonrpc_begin_request( - struct spdk_jsonrpc_client_request *request, - const char *method, - int32_t id); +struct spdk_json_write_ctx * +spdk_jsonrpc_begin_request(struct spdk_jsonrpc_client_request *request, int32_t id, + const char *method); /** * Complete a JSON-RPC request. diff --git a/lib/jsonrpc/jsonrpc_client.c b/lib/jsonrpc/jsonrpc_client.c index e605dd50b..d2cf22068 100644 --- a/lib/jsonrpc/jsonrpc_client.c +++ b/lib/jsonrpc/jsonrpc_client.c @@ -167,8 +167,8 @@ jsonrpc_client_write_cb(void *cb_ctx, const void *data, size_t size) } struct spdk_json_write_ctx * -spdk_jsonrpc_begin_request(struct spdk_jsonrpc_client_request *request, const char *method, - int32_t id) +spdk_jsonrpc_begin_request(struct spdk_jsonrpc_client_request *request, int32_t id, + const char *method) { struct spdk_json_write_ctx *w; @@ -179,8 +179,14 @@ spdk_jsonrpc_begin_request(struct spdk_jsonrpc_client_request *request, const ch spdk_json_write_object_begin(w); spdk_json_write_named_string(w, "jsonrpc", "2.0"); - spdk_json_write_named_int32(w, "id", id); - spdk_json_write_named_string(w, "method", method); + + if (id >= 0) { + spdk_json_write_named_int32(w, "id", id); + } + + if (method) { + spdk_json_write_named_string(w, "method", method); + } return w; } diff --git a/test/rpc_client/rpc_client_test.c b/test/rpc_client/rpc_client_test.c index a12533c89..68f847132 100644 --- a/test/rpc_client/rpc_client_test.c +++ b/test/rpc_client/rpc_client_test.c @@ -70,7 +70,7 @@ spdk_jsonrpc_client_check_rpc_method(struct spdk_jsonrpc_client *client, char *m return -ENOMEM; } - w = spdk_jsonrpc_begin_request(request, "get_rpc_methods", 1); + w = spdk_jsonrpc_begin_request(request, 1, "get_rpc_methods"); spdk_jsonrpc_end_request(request, w); spdk_jsonrpc_client_send_request(client, request); spdk_jsonrpc_client_free_request(request);