RejectedSoftware Forums

Sign up

I Don't know if this is a bug or I'm doing something wrong...

I've a problem where the RestInterfaceClient generator is not working but CURL is. That's not a big problem for me because this API is going to be read externally (from javascript calls) but I found it odd and maybe worth reporting, so here it goes:

I've reduced this to a reproducible case:

module app;                                                                                                                                                                                          import vibe.d;
import vibe.core.log;                                                                                                                                                                                
@rootPathFromName
interface Api
{
    @method(HTTPMethod.GET) @path("something/")
    string getSomething(string id);
}

class ApiImpl: Api
{
    override:
        string getSomething(string id)
        {
            return "hello word";
        }
}

shared static this()
{
    setLogLevel(LogLevel.debugV);
    auto router = new URLRouter;
    router.registerRestInterface(new ApiImpl);
    auto settings = new HTTPServerSettings;
    settings.port = 8080;
    settings.bindAddresses = ["127.0.0.1"];
    listenHTTP(settings, router);
}

unittest
{
    auto apiClient = new RestInterfaceClient!Api("http://127.0.0.1:8080");
    auto conv1 = apiClient.getSomething("someid");
}

If I use curl to do:

curl -X GET -H "Content-Type: application/json" "http://localhost:8080/api/someid/something/"

It returns the "hello world" as expected. But if I do a dub test, Vibed shows this 404 where you can see that the URL call miss the :id argument:

no route match: ==>GET /api/something/<==<br>No response written for /api/something/<br>REST call: GET http://127.0.0.1:8080/api/something/ -> 404, 404<br>Error while handling response: vibe.web.common.RestException@../../../../../../.dub/packages/vibe-d-master/source/vibe/web/rest.d(377): Not Found (404)

Adding more arguments to getSomething other than an id works if I pass them in the URL with the ?key=value&anotherkey=anothervalue format.

Re: I Don't know if this is a bug or I'm doing something wrong...

PS: just checked with an "int id" parameter just to discard that the problem was using a "string id". Same result.

Re: I Don't know if this is a bug or I'm doing something wrong...

PS2: the unnitest on rest.d, line 388 doesnt test the method "getName".

Re: I Don't know if this is a bug or I'm doing something wrong...

On Mon, 21 Jul 2014 12:00:49 GMT, Juanjo Alvarez wrote:

I've a problem where the RestInterfaceClient generator is not working but CURL is. That's not a big problem for me because this API is going to be read externally (from javascript calls) but I found it odd and maybe worth reporting, so here it goes:

I've reduced this to a reproducible case:

module app;                                                                                                                                                                                          import vibe.d;
import vibe.core.log;                                                                                                                                                                                
@rootPathFromName
interface Api
{
    @method(HTTPMethod.GET) @path("something/")
    string getSomething(string id);
}

class ApiImpl: Api
{
    override:
        string getSomething(string id)
        {
            return "hello word";
        }
}

shared static this()
{
    setLogLevel(LogLevel.debugV);
    auto router = new URLRouter;
    router.registerRestInterface(new ApiImpl);
    auto settings = new HTTPServerSettings;
    settings.port = 8080;
    settings.bindAddresses = ["127.0.0.1"];
    listenHTTP(settings, router);
}

unittest
{
    auto apiClient = new RestInterfaceClient!Api("http://127.0.0.1:8080");
    auto conv1 = apiClient.getSomething("someid");
}

If I use curl to do:

curl -X GET -H "Content-Type: application/json" "http://localhost:8080/api/someid/something/"

It returns the "hello world" as expected. But if I do a dub test, Vibed shows this 404 where you can see that the URL call miss the :id argument:

no route match: ==>GET /api/something/<==<br>No response written for /api/something/<br>REST call: GET http://127.0.0.1:8080/api/something/ -> 404, 404<br>Error while handling response: vibe.web.common.RestException@../../../../../../.dub/packages/vibe-d-master/source/vibe/web/rest.d(377): Not Found (404)

Adding more arguments to getSomething other than an id works if I pass them in the URL with the ?key=value&anotherkey=anothervalue format.

I think the issue here is that RestInterfaceClient doesn't support the @path attribute together with id parameters or :placeholders. I'll open a ticket for that.

In this particular case, I think just dropping the @path attribute should make it work (because GET and "/something" are already inferred from the method name).

Re: I Don't know if this is a bug or I'm doing something wrong...

On Mon, 21 Jul 2014 12:47:05 GMT, Juanjo Álvarez wrote:

PS2: the unnitest on rest.d, line 388 doesnt test the method "getName".

Unfortunately that unit test is only a documentation example and doesn't actually run at all. But it would be a good idea to add an integration test in the tests/ folder. I'll copy something from the unit tests there.

Ticket for the original issue: #738

Re: I Don't know if this is a bug or I'm doing something wrong...

On Tue, 22 Jul 2014 08:15:20 GMT, Sönke Ludwig wrote:

On Mon, 21 Jul 2014 12:47:05 GMT, Juanjo Álvarez wrote:

PS2: the unnitest on rest.d, line 388 doesnt test the method "getName".

Unfortunately that unit test is only a documentation example and doesn't actually run at all. But it would be a good idea to add an integration test in the tests/ folder. I'll copy something from the unit tests there.

Ticket for the original issue: #738

Fixed and test case added: d307356

Re: I Don't know if this is a bug or I'm doing something wrong...

On Tue, 22 Jul 2014 08:42:35 GMT, Sönke Ludwig wrote:

Fixed and test case added: d307356

Works beautifully now, thanks!