RejectedSoftware Forums

Sign up

REST and JSON

Hello.

I have a dumb question.
I'm trying send serialized string to REST interface. Is it possible?

There is a simple example:

class test
{
	uint id;
}

class MainAPI : IRest 
{
	private test[] m_test;

	void addTest( test added ) 
	{
		m_test ~= added;
	}

}

And i'm trying like this:

E:\Users\zstas>curl -XPOST  -H "Content-Type: application/json" http://127.0.0.1/api/test -d {\"id\":1}

And get this respone:

500 - Internal Server Error

Internal Server Error

Internal error information:
object.Exception@source/vibe/http/rest.d(677): Missing parameter added
----------------
0x004F526A in pure @safe void std.exception.bailOut(immutable(char)[], uint, const(char[]))
0x004A71B6 in D4vibe4http4rest126__T17jsonMethodHandlerTC8exchange3api5IRestVAyaa8_6164644c3276706eS56_D8exchA4949775D86E63AFF1CE1AE9A4C8127F at e:\proj\man\source/vibe/http/rest.d(679)
0x004A8364 in void vibe.http.router.URLRouter.handleRequest(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse) at E:\Users\zstas\AppData\Roaming\dub\packages\vibe-d-0.7.18-rc.2\source\vibe\http\router.d(160)
0x004ACD72 in bool vibe.http.server.handleRequest(vibe.core.stream.Stream, vibe.core.net.TCPConnection, vibe.http.server.HTTPServerListener, ref vibe.http.server.HTTPServerSettings, ref bool) at E:\Users\zstas\AppData\Roaming\dub\packages\vibe-d-0.7.18-rc.2\source\vibe\http\server.d(1389)
0x004ABC60 in void vibe.http.server.handleHTTPConnection(vibe.core.net.TCPConnection, vibe.http.server.HTTPServerListener) at E:\Users\zstas\AppData\Roaming\dub\packages\vibe-d-0.7.18-rc.2\source\vibe\http\server.d(1178)
0x004A8E5D in D4vibe4http6server15listenHTTPPlainFC4vibe4http6server18HTTPServerSettingsDFC4vibe4http6server16411A618C3CE4E141DF817A61FEBFC1C at E:\Users\zstas\AppData\Roaming\dub\packages\vibe-d-0.7.18-rc.2\source\vibe\http\server.d(124)
0x00468238 in void vibe.core.drivers.libevent2_tcp.onConnect(int, short, void*).ClientTask.execute() at E:\Users\zstas\AppData\Roaming\dub\packages\vibe-d-0.7.18-rc.2\source\vibe\core\drivers\libevent2_tcp.d(452)
0x0045BC17 in void vibe.core.core.CoreTask.run() at E:\Users\zstas\AppData\Roaming\dub\packages\vibe-d-0.7.18-rc.2\source\vibe\core\core.d(605)
0x004FDB45 in void core.thread.Fiber.run()
0xFFFFFFFF
0x7736D74D in RtlAddMandatoryAce

I could get JSON (like addTest( JSON added) but then my method will consist of only one action - deserialization.
Sorry for bad english.

Re: REST and JSON

On Sun, 24 Nov 2013 08:44:41 GMT, Stanislav wrote:

Hello.

I have a dumb question.
I'm trying send serialized string to REST interface. Is it possible?

There is a simple example:

class test
{
	uint id;
}

class MainAPI : IRest 
{
	private test[] m_test;

	void addTest( test added ) 
	{
		m_test ~= added;
	}

}

And i'm trying like this:

E:\Users\zstas>curl -XPOST  -H "Content-Type: application/json" http://127.0.0.1/api/test -d {\"id\":1}

And get this respone:

500 - Internal Server Error
(...)

I could get JSON (like addTest( JSON added) but then my method will consist of only one action - deserialization.
Sorry for bad english.

In the current form, the POST body would have to look like

{"added": {"id": 1}}

To get a custom JSON serialization, toJson/fromJson can be used:

class Test {
	uint id;
	Json toJson() const { return Json(id); }
	static Test fromJson(Json value) { return Test(value.get!uint); }
}

So the body becomes

{"added": 1}

Naming the first parameter "id" in the JSON representation currently doesn't work due to the special meaning it has (it will be deprecated any removed at some point, though).

Re: REST and JSON

In the current form, the POST body would have to look like

{"added": {"id": 1}}

Thank you. It is exactly what i want.