Sathya
Sathya

Reputation: 58

Creating a GET method with request body in kotlin using ARest Framework

I am setting up a new service consisting of GET, DELETE and POST method APIs using ARest framework in kotlin.

I am wrapping up the inputs in a data class and passing it out to the methods. In this case DELETE and POST method works fine but I face some problem with the GET method.

Data class for wrapping the input :

class GetRequest(@QueryParam("aaa") var aaa: String? = null,
                                        @QueryParam("bbb") var bbb: String? = null,
                                        @QueryParam("ccc") var ccc: UserDefinedType? = null)

Model definition :

@GET
@Path("getStatus/")
@Produces(MediaType.APPLICATION_JSON)
fun getStatus(@NotNull @BeanParam getRequest: GetRequest) : GetResponse

I use swagger to call the methods, Body of the request :

{
  "aaa": "string",
  "bbb": "string",
  "ccc": "HEAD"
}

My understanding is that, @BeanParam will inject the corresponding parameters from the query into the data class. But from swagger I find the request URL as, https://:8090/api/getStatus and couldn't find any query params. Because of which the value of "aaa" remains null instead of getting changed to "string". Could someone help me in pointing out the mistake I made here?

Upvotes: 1

Views: 812

Answers (1)

user3738870
user3738870

Reputation: 1632

The resource and data class expects parameters as query parameters, but you send them in the body. You should send them as query parameters instead (getStatus?aaa=string&bbb=string&ccc=HEAD), or if you want to send it in the body (which is not recommended for a GET request), you have to modify your code, for example:

@GET
@Path("getStatus/")
@Produces(MediaType.APPLICATION_JSON)
fun getStatus(getRequest: GetRequest) : GetResponse

class GetRequest(var aaa: String? = null,
                 var bbb: String? = null,
                 var ccc: UserDefinedType? = null)

Upvotes: 0

Related Questions