RejectedSoftware Forums

Sign up

The request body must contain a JSON object with an entry for each parameter?

Hi guys. I'm in trouble once more :).
I'm trying to implement a POST method in my REST Api service. Here's the code:

@rootPathFromName
public interface IWeb
{
	@path("/authenticate") @method(HTTPMethod.POST) @bodyParam("credential", "credential")
	string authenticate(Credential credential);
}
	public string authenticate(Credential credential)
	{
		return credential.username ~ "\t" ~ credential.password;
	}

However, when I try to POST the following JSON:

{
    "username": "Dato",
    "password": "asd"
}

I get an error saying "Missing parameter credential".

Then I tried this:

"credential":
{
    "username": "Dato",
    "password": "asd"
}

but now I get "The request body must contain a JSON object with an entry for each parameter.". Any idea what's wrong with the code? I use application/json as a Content-Param value.

Thanks :)

Re: The request body must contain a JSON object with an entry for each parameter?

What about this?

{
  "credential": {
    "username": "Dato",
    "password": "asd"
  }
}

BTW, you can drop most of the attributes, as those are the defaults anyway:

@rootPathFromName
interface IWeb {
  // POST /authenticate {"credential": ...}
  string authenticate(Credential credential);
}

To get the first POST body to work ({"username": "Dato", "password": "asd"}), you could use string authenticate(string username, string password); - or string authenticate(ValidUsername username, ValidPassword password); to get some automatic input validation.

Re: The request body must contain a JSON object with an entry for each parameter?

You saved my day again and again. Thank you very much :)
btw, where can I find more info about validation? :)

Thanks again :)

On Sat, 02 May 2015 16:47:44 GMT, Sönke Ludwig wrote:

What about this?

{
  "credential": {
    "username": "Dato",
    "password": "asd"
  }
}

BTW, you can drop most of the attributes, as those are the defaults anyway:

@rootPathFromName
interface IWeb {
  // POST /authenticate {"credential": ...}
  string authenticate(Credential credential);
}

To get the first POST body to work ({"username": "Dato", "password": "asd"}), you could use string authenticate(string username, string password); - or string authenticate(ValidUsername username, ValidPassword password); to get some automatic input validation.

Re: The request body must contain a JSON object with an entry for each parameter?

There are some examples in the API docs, but this was a relatively recent addition and can still use some more features and better documentation.

On Sat, 02 May 2015 20:41:33 GMT, Dato wrote:

You saved my day again and again. Thank you very much :)
btw, where can I find more info about validation? :)

Thanks again :)