Hi Folks,
Currently I am trying to get used to vibe.d REST framework, but have a
couple of questions.
I want to POST some JSON data through the API, but looks like I need to
specify all possible parameters with the function, for example:
import vibe.appmain;
import vibe.core.core;
import vibe.http.rest;
import vibe.http.router;
import vibe.http.server;
import vibe.data.json;
@rootPathFromName
interface API
{
@path("register") @method(HTTPMethod.POST)
Json postRegister(string a = null, int b = 0);
}
class MyAPI: API
{
override:
Json postRegister(string a, int b)
{
Json j = Json.emptyObject;
if (a !is null)
j["a"] = a;
return j;
}
}
shared static this()
{
auto settings = new HTTPServerSettings;
settings.port = 8080;
auto routes = new URLRouter;
registerRestInterface(routes, new MyAPI);
listenHTTP(settings, routes);
}
This looks OK, but suppose I have many fields associated with DB table
in backend, or I have sub-object in the JSON data, I would prefer to
process the POSTed data inside the function, not as function parameters.
So I wonder if I could get Json as a parameter.
The other thing is, with above example, if I want to set 'null' to field
"a", like {"a":null}, it doesn't accept it, like:
curl -k -H 'content-type: application/json' -X POST -d
'{"a":null,"b":1}' 'http://localhost:8080/api/register'
500 - Internal Server Error
Internal Server Error
Internal error information:
std.json.JSONException@/home/build/tmp/nativebuild/.build/src/gcc-4.9.0/libphobos/src/std/json.d(842):
Got JSON of type null, expected string.
There are two issues here. First I cannot set 'null' explicitly in any
fields. Second, for integer value, apparently it does not allow 'null'
as default in function declaration, so if I don't put the field in
POST/PUT data, it will fall back to default integer value, while I
expect to leave the value if unspecified.
Thanks,
--Toshiaki