JSON API accessible via HTTP GET method

This is useful in cases where the client cannot use POST method.
/jsonApiServer/shutdown now reply before shutting down the core.
This commit is contained in:
Gioacchino Mazzurco 2018-09-12 23:40:34 +02:00
parent 3d476f689f
commit 148e46346d
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
2 changed files with 39 additions and 5 deletions

View File

@ -55,8 +55,7 @@ extern RsGxsChannels* rsGxsChannels;
.Calling the JSON API with curl on the terminal
[source,bash]
--------------------------------------------------------------------------------
curl -H "Content-Type: application/json" --data @paramethers.json \
http://127.0.0.1:9092/rsGxsChannels/createGroup
curl --data @paramethers.json http://127.0.0.1:9092/rsGxsChannels/createGroup
--------------------------------------------------------------------------------
.JSON API call result
@ -69,6 +68,28 @@ curl -H "Content-Type: application/json" --data @paramethers.json \
}
--------------------------------------------------------------------------------
Even if it is less efficient because of URL encoding HTTP +GET+ method is
supported too, so in cases where the client cannot use +POST+ she can still use
+GET+ taking care of encoding the JSON data. With +curl+ this can be done at
least in two different ways.
.Calling the JSON API with GET method with curl on the terminal
[source,bash]
--------------------------------------------------------------------------------
curl --get --data-urlencode jsonData@paramethers.json \
http://127.0.0.1:9092/rsGxsChannels/createGroup
--------------------------------------------------------------------------------
Letting +curl+ do the encoding is much more elegant but it is semantically
equivalent to the following.
.Calling the JSON API with GET method and pre-encoded data with curl on the terminal
--------------------------------------------------------------------------------
curl http://127.0.0.1:9092/rsGxsChannels/createGroup?jsonData=%7B%0A%20%20%20%20%22group%22%3A%7B%0A%20%20%20%20%20%20%20%20%22mMeta%22%3A%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mGroupName%22%3A%22JSON%20test%20group%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mGroupFlags%22%3A4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mSignFlags%22%3A520%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22mDescription%22%3A%22JSON%20test%20group%20description%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22caller_data%22%3A%22Here%20can%20go%20any%20kind%20of%20JSON%20data%20%28even%20objects%29%20that%20the%20caller%20want%20to%20get%20back%20together%20with%20the%20response%22%0A%7D
--------------------------------------------------------------------------------
Note that using +GET+ method +?jsonData=+ and then the JSON data URL encoded are
added after the path in the HTTP request.
== Offer new RetroShare services through JSON API

View File

@ -36,6 +36,12 @@
nullptr, 0, \
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING ); \
RsJson& jReq(cReq.mJson); \
if(session->get_request()->get_method() == "GET") \
{ \
const std::string jrqp(session->get_request()->get_query_parameter("jsonData")); \
jReq.Parse(jrqp.c_str(), jrqp.size()); \
} \
else \
jReq.Parse(reinterpret_cast<const char*>(body.data()), body.size()); \
\
RsGenericSerializer::SerializeContext cAns; \
@ -87,8 +93,15 @@ JsonApiServer::JsonApiServer(
registerHandler("/jsonApiServer/shutdown",
[this](const std::shared_ptr<rb::Session> session)
{
session->close(rb::OK);
size_t reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( reqSize, [this](
const std::shared_ptr<rb::Session> session,
const rb::Bytes& body )
{
INITIALIZE_API_CALL_JSON_CONTEXT;
DEFAULT_API_CALL_JSON_RETURN(rb::OK);
shutdown();
} );
});
registerHandler("/jsonApiServer/version",