mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-12-29 01:16:20 -05:00
Merge pull request #1337 from G10h4ck/jsonapi
Make JSON API accessible via HTTP GET method
This commit is contained in:
commit
7784660d67
@ -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
|
||||
|
||||
|
@ -36,7 +36,13 @@
|
||||
nullptr, 0, \
|
||||
RsGenericSerializer::SERIALIZATION_FLAG_YIELDING ); \
|
||||
RsJson& jReq(cReq.mJson); \
|
||||
jReq.Parse(reinterpret_cast<const char*>(body.data()), body.size()); \
|
||||
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; \
|
||||
RsJson& jAns(cAns.mJson); \
|
||||
@ -87,8 +93,15 @@ JsonApiServer::JsonApiServer(
|
||||
registerHandler("/jsonApiServer/shutdown",
|
||||
[this](const std::shared_ptr<rb::Session> session)
|
||||
{
|
||||
session->close(rb::OK);
|
||||
shutdown();
|
||||
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",
|
||||
|
@ -51,6 +51,8 @@
|
||||
#ifdef RS_JSONAPI
|
||||
# include <csignal>
|
||||
# include "jsonapi/jsonapi.h"
|
||||
# include "util/rsnet.h"
|
||||
# include "util/rsurl.h"
|
||||
#endif // RS_JSONAPI
|
||||
|
||||
/* Basic instructions for running libretroshare as background thread.
|
||||
@ -91,6 +93,16 @@ int main(int argc, char **argv)
|
||||
[](int /*ec*/) { std::raise(SIGTERM); } );
|
||||
|
||||
jsonApiServer->start("JSON API Server");
|
||||
|
||||
sockaddr_storage tmp;
|
||||
sockaddr_storage_inet_pton(tmp, jsonApiBindAddress);
|
||||
sockaddr_storage_setport(tmp, jsonApiPort);
|
||||
sockaddr_storage_ipv6_to_ipv4(tmp);
|
||||
RsUrl tmpUrl(sockaddr_storage_tostring(tmp));
|
||||
tmpUrl.setScheme("http");
|
||||
|
||||
std::cerr << "JSON API listening on " << tmpUrl.toString()
|
||||
<< std::endl;
|
||||
}
|
||||
#endif // RS_JSONAPI
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user