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).