Srikanth
Srikanth

Reputation: 836

Error when calling the ApiMethod with optional parameters

I have created a ApiMethod and marked couple of arguments as @Nullable. From my client webpage when i am making a request its throwing the below error. It was worked fine when i tested the samples (Greetings.java). But not in my API. Any ideas what I am missing?

{
  "error" : {
    "message" : "java.lang.IllegalArgumentException",
    "code" : 400,
    "errors" : [ {
      "domain" : "global",
      "reason" : "badRequest",
      "message" : "java.lang.IllegalArgumentException"
    } ]
  }
}

Here is my code samples:

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.Named;
import com.google.api.server.spi.config.Nullable;

@ApiMethod(name = "adddetails", path = "admin/adddetails")
public Details addDetails(
        @Named("date") long date,
        @Named("hours") int hours,
        @Named("weekend") @Nullable boolean weekend,
        @Named("holiday") @Nullable boolean holiday

Inside my webpage (html page)

var request = api.adddetails({
            "date": date.getTime(),
            "hours": 0
        });
        request.execute();

If I ran below lines then the ApiMethod is called without any errors:

    var request = api.adddetails({
            "date": date.getTime(),
            "hours": 0,
            "weekend": row.weekend,
            "holiday": row.holiday
        });
        request.execute();

I have checked the myapi.api file and inside that it shows the parameters as "required" : false so that mean i am received the correct api on to client side. But not sure whats going wrong.

I am testing on localhost.

Upvotes: 0

Views: 149

Answers (1)

saiyr
saiyr

Reputation: 2595

Use Boolean, not boolean for parameters marked Nullable. There should probably be a warning for using it with primitives, which of course, can't be null.

Upvotes: 1

Related Questions