RejectedSoftware Forums

Sign up

Vibe REST Post Error

Hello,

I'm having a bit of trouble being able to do a POST. My get
works (getStatus), but the postCalc method keeps throwing an
error for some reason. From what I can tell, my code is valid
and I've tried various permutations just to get something to work
to no avail. My goal is to find a way to accept two or more
parameters and return a result.

Code:

import vibe.d;

class TestInterface
{

  string getStatus() { return "running"; }
  int postCalc(int val1, int val2)
  {
      return val1 + val2;
  }

}

static this()
{

  auto router = new UrlRouter;

  registerRestInterface(router, new TestInterface, "/");

  auto settings = new HttpServerSettings;
  settings.port = 8080;

  listenHttp(settings, router);

}

Curl command:

curl -i -XPOST -H "Content-Type: text/json"
http://localhost:8080/calc -d '{"val1": 1, "val2": 2}'

And the error:

500 - Internal Server Error

Internal Server Error

Internal error information:
object.Exception@/home/dev/dev/vibe.d/source/vibe/data/json.d(609):
Trying to access JSON of type Undefined as Json[string].

/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(pure
@safe bool std.exception.enforce!(bool).enforce(bool, lazy
const(char)[], immutable(char)], uint)+0x2c) [0x81a93d8]
/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(const(void
function())
vibe.data.json.Json.checkType!(vibe.data.json.Json[immutable(char)[]]).heckType+0x3f)
[0x81a9553]
/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(ref
vibe.data.json.Json
vibe.data.json.Json.opIndex(immutable(char)[])+0x3c) [0x81a7c98]
/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(void
delegate(vibe.http.server.HttpServerRequest,
vibe.http.server.HttpServerResponse)
vibe.htp.rest.jsonMethodHandler!(app.TestInterface, "postCalc",
int function(int,
int)).jsonMethodHandler(app.TestInterface).void<br>handler(vibe.http.server.HttpServeRequest,<br>vibe.http.server.HttpServerResponse)+0x145) [0x81da48d]<br>/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(void<br>vibe.http.router.UrlRouter.handleRequest(vibe.http.server.HttpServerRequest,<br>vibe.http.sever.HttpServerResponse)+0x120) [0x8169680]<br>/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(bool<br>vibe.http.server.handleRequest(vibe.stream.stream.Stream,<br>immutable(char)[], vibe.http.sever.HTTPServerListener, ref<br>vibe.http.server.HttpServerSettings)+0xdec) [0x81b5ce0]<br>/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(void<br>vibe.http.server.handleHttpConnection(vibe.core.driver.TcpConnection,<br>vibe.http.server.HTPServerListener)+0x143) [0x81b4e7b]<br>/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(void<br>vibe.http.server.listenHttpPlain(vibe.http.server.HttpServerSettings,<br>void delegate(vibe.ttp.server.HttpServerRequest,<br>vibe.http.server.HttpServerResponse)).void<br>doListen(vibe.http.server.HttpServerSettings,<br>vibe.http.server.HTTPServerListener, imutable(char)[]).void<br>__lambda20(vibe.core.driver.TcpConnection)+0x27) [0x81b2c47]<br>/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(extern<br>(C) void vibe.core.drivers.libevent2_tcp.onConnect(int, short,<br>void).void ClientTask.eecute()+0x200) [0x81e7054]
/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(void
vibe.core.core.CoreTask.run()+0x5d) [0x81ba061]
/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(void
core.thread.Fiber.run()+0x21) [0x8212e61]
/tmp/.rdmd-1000/rdmd-app.d-BE457B9C3E08EC5E9E05D1C46EE22220/app(fiber_entryPoint+0x4e)
[0x8212d9e]

Re: Vibe REST Post Error

Am 30.08.2012 20:28, schrieb Casey:

Curl command:

curl -i -XPOST -H "Content-Type: text/json"
http://localhost:8080/calc -d '{"val1": 1, "val2": 2}'

The correct mime type is "application/json" - don't ask me why, my
initial guess was also text/json ;)

I'll add an enforcement that gives a proper hint though, as the current
error message obviously does not help.

Re: Vibe REST Post Error

Thanks! That worked just as expected.