RejectedSoftware Forums

Sign up

REST Client POST Object

I trying to write rest api client with vibe.d rest generator and figured that the such code:

class App
{
    string id;
    string cmd;
}

interface IntREST
{
    @path("/apps")
    @method(HTTPMethod.POST)
    App postApp(App app);
}

sends body like:

{
    "app": {
        "id": "value",
        "cmd": "command"
    }
}

but I need to send without nesting into "app":

{
    "id": "value",
    "cmd": "command"
}

How I can avoid such behavior like nesting?

Re: REST Client POST Object

Up!

Re: REST Client POST Object

On Mon, 13 Jun 2016 19:48:05 GMT, zunkree wrote:

I trying to write rest api client with vibe.d rest generator and figured that the such code:

class App
{
    string id;
    string cmd;
}

interface IntREST
{
    @path("/apps")
    @method(HTTPMethod.POST)
    App postApp(App app);
}

sends body like:

{
    "app": {
        "id": "value",
        "cmd": "command"
    }
}

but I need to send without nesting into "app":

{
    "id": "value",
    "cmd": "command"
}

How I can avoid such behavior like nesting?

Currently there is no way, but we could do something like specializing @bodyParam with an empty field name:

@bodyParam("app", "")
App postApps(App app);

It would then error out of there are other body parameters and otherwise would write the contents directly into the response body.

Re: REST Client POST Object

On Mon, 20 Jun 2016 17:26:28 GMT, Sönke Ludwig wrote:

Currently there is no way, but we could do something like specializing @bodyParam with an empty field name:

@bodyParam("app", "")
App postApps(App app);

It would then error out of there are other body parameters and otherwise would write the contents directly into the response body.

Great, but I'm not sure that the @bodyParam("app", "") is a good idea because

{
  "": "value"
}

is valid JSON object. Maybe @bodyParam("app") will be better in this case. What do you think?

Re: REST Client POST Object

On Tue, 21 Jun 2016 12:15:38 GMT, zunkree wrote:

On Mon, 20 Jun 2016 17:26:28 GMT, Sönke Ludwig wrote:

Currently there is no way, but we could do something like specializing @bodyParam with an empty field name:

@bodyParam("app", "")
App postApps(App app);

It would then error out of there are other body parameters and otherwise would write the contents directly into the response body.

Great, but I'm not sure that the @bodyParam("app", "") is a good idea because

{
  "": "value"
}

is valid JSON object. Maybe @bodyParam("app") will be better in this case. What do you think?

Yeah, you are probably right. I was thinking about using a default parameter for the second one when writing ("app", ""), but a separate overload with a distinct return value is a better idea.