1

I need to send a POST with Retrofit:

Retrofit.Builder()
  .baseUrl(API_BASE_URL)
  .addConverterFactory(
      GsonConverterFactory.create(
          GsonBuilder().serializeNulls().create()
      ))
  .client(okHttpClient)
  .build()

    @POST(endpoint)
    @FormUrlEncoded
    fun partialPickupsDelivery(
            @HeaderMap headers: Map<String, String>,
            @Field("userId") userId: String,
            @Field("purchaseIds[]") purchases: ArrayList<String>
    ): Call<ResponseBody>

The server is waiting something like this: purchaseIds=["1234", "2345", "3456"]

The problem comes when I try to send an empty array (not null) because the server is waiting: purchaseIds=[] But I'm sending only the first param userId (I think retrofit is removing the second one because the array is empty).

Is there any way to send purchaseIds=[]?

Thx

6
  • Can you show how this function partialPickupsDelivery is called? Commented May 21, 2020 at 8:52
  • @Field("purchaseIds[]") this is not the way of calling array. Commented May 21, 2020 at 8:54
  • Try this @Field("purchaseIds") purchases = emptyList<String> Commented May 21, 2020 at 8:56
  • service.partialPickupsDelivery( headers, userId, purchaseId ) where headers: Map<String, String> userId: String purchaseIds: ArrayList<String> Commented May 21, 2020 at 8:57
  • @CôngHải: at the request is still removed this field Commented May 21, 2020 at 9:06

2 Answers 2

1

I finally found a solution that works for me:

@POST(endpoint)
fun partialPickupsDelivery(
   @HeaderMap headers: Map<String, String>,
   @Body params: HashMap<String, Any>
): Call<ResponseBody?>

where params is:

val params = HashMap<String, Any>().apply {
    this["purchaseIds"] = destinationsId
    this["userId"] = deliverId
}

and it's called this way:

val call: Call<ResponseBody> =
      service.partialPickupsDelivery(
          headers,
          params
      )

I hope this can help someone

Sign up to request clarification or add additional context in comments.

Comments

0

you can use List instead of ArrayList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.