www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - vibe.d get parameters

reply Brian Forbes <brian.patrick.forbes gmail.com> writes:
     I have been programming in D now for a few months, and I am 
really impressed by how easy it is to get things up and running.
     However, I have run into some problems with vibe.d. I can't 
seem to figure out how to pass a get parameter after searching 
online for a few days.

     The code is more or less as below. I have tried lots of 
variations on how to add an input parameter (using something/:id 
etc) but everything gives a 404 error.

     Any help would be greatly appreciated. Thanks !

http://localhost:8081/something?id=a ( <---- this gives 404 )

import vibe.d;
import std.stdio;
import std.conv;
import std.json;

shared static this()
	auto router = new URLRouter;

	router.registerRestInterface(new ApiImpl);

	auto settings = new HTTPServerSettings;
	settings.port = 8081;
	listenHTTP(settings, router);

	logInfo("Please open in your browser.");

  interface Api
       method(HTTPMethod.GET)  path("/something/")
      string getSomething(string id);

  class ApiImpl: Api
          string getSomething(string id)
              return id ~ "\nhello word";
May 05 2016
parent Dicebot <public dicebot.lv> writes:
There are two problems here.

1) Your problem comes from the fact that `id` parameter name has 
been special cased since early vibe.d versions to mean URL 
parameter. If you enable `setLogLevel(LogLevel.debug_);` in the 
very beginning of module constructor, you will see this trace:

REST route: GET /:id/something/ []
add route GET /:id/something/

It basically automatically rewrites route to prepend :id and 
treats `string id` as if it was `string _id`.

2) Changing the name of string argument to `xxx` still doesn't 
work and now prints this in debug trace:

REST route: GET /something/ ["xxx"]
add route GET /something/

Note the / in the end. Vibe.d does exact matching of slashes in 
the version I have checked so you would have to request to get response.

So to make URL you want to work you'd need to do something like 

interface Api
      method(HTTPMethod.GET)  path("/something")
     string getSomething(string xxx);
May 05 2016