Sat
Sat

Reputation: 4158

How can we pass java object to groovyx.net.http.RestClient's POST call

I have written a Restfull webservice POST api call, this api is accepting java object as request parameter

Sample code:

@POST
@Path("/sample")
@ApiOperation(value = "insert sample data",
        notes = "insert sample data", response = SampleRequest.class)
public Response processSampleData(@ApiParam(value = "SampleRequest", required = true) SampleRequest sampleRequest) {
   //code to insert data
}

I am writing an Integration test method, but not able to pass java object to the RestClient

Sample test method:

def "process sample data"(){
    when:
    /*String json = '{"sampleDataList":[{ "name": "test1", "id": "12345" },{ "name": "test2", "id": "123456"}]}'*/
    Sample sample= new Sample();
    sample.setName("test1");
    sample.setId("12345");
    SampleRequest reqObj = new SampleRequest();
    reqObj.getSampleList().add(sample);

    //tried with json
    /*HttpResponseDecorator response = getRestClient().post([path: "$BASE_URL"+"/sample", body: json])*/

    //tried with java object also
    HttpResponseDecorator response = getRestClient().post([path: "$BASE_URL"+"/sample", body: SampleRequest])

    then:
    response
}

I tried using both json and java object but none of them worked for me. Getting No encoder found for request content type */* error

Upvotes: 1

Views: 641

Answers (1)

Sat
Sat

Reputation: 4158

When I added contentType: "application/json then it worked for json body type.

I changed

HttpResponseDecorator response = getRestClient().post([path: "$BASE_URL"+"/sample",body: json])

To

HttpResponseDecorator response = getRestClient().post([path: "$BASE_URL"+"/sample",contentType: "application/json" ,body: json])

Upvotes: 3

Related Questions