-1

I am referring this SO Thread but unable to generate proper api request. api request is as below

Request :

{
  "clientId": "v@gmail.com",
  "products": {
    "productNumber": "8123456789123",
    "batchNumber": "123456789",
    "quantity": 50,
    "salesPrice": 500,
    "purchasePrice": 500,
    "MRP": 600,
    "mfgDate": "15/05/2017",
    "expDate": "15/05/2017"
  }
}

Api Interface

@FormUrlEncoded
@POST("/products/addBatch")
public Observable<AddBatchResponse> doAddBatch(@Header(WebServicesTags.TAG_AUTHORIZATION) String token,
                                               @Field(WebServicesTags.TAG_CLIENT_ID) String clientId,
                                               @Body BatchModel product);

and following is my Api call

BatchModel model = new BatchModel(productNumber, batchNumber, quantity, salesPrice, purchasePrice, mRP, mfgDate, expDate);
Observable<AddBatchResponse> addBatch = apiservice.doAddBatch(sessionManager.getKeyToken(), sessionManager.getKeyEmail(), model);
        addBatch.subscribeOn(Schedulers.newThread())

call this api shows me below Error

java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding.

karan
  • 8,637
  • 3
  • 41
  • 78

1 Answers1

1

you can either use @Field with @FormUrlEncoded or @Body.. you cant use both at a same time

Do something like

@POST("/products/addBatch")
public Observable<AddBatchResponse> doAddBatch(@Header(WebServicesTags.TAG_AUTHORIZATION) String token,
                                           @Body HashMap<String, Object> map);

and while posting data create a map

HashMap<String,Object> map  = new HashMap<String, Object>();
map.put("clientId","v@gmail.com");
map.put("product", batchModel);

Pass this map as the body parameter to the api call I hope this will solve your problem

Tabish Hussain
  • 852
  • 5
  • 13