L
L
lovesuper2014-11-27 12:34:09
Java
lovesuper, 2014-11-27 12:34:09

[Java, Retrofit] Is there a way to send multiple fields with the same name to the server in the POST body?

Client development uses Retrofit.
The situation is this: in the POST body, the validator on the server expects several fields with the same name among many, for example:

  • age: 20
  • name: Vasya
  • location: 1
  • location: 2
  • location: 3

etc. When trying to solve the problem head-on, it turns out like this:
@Multipart
    post("/users")
    void create(
            @ Part("name") String name,
            @ Part("age") String age,
            @ Part("place") List<Integer> place,

But it turns out that Retrofit sends place '1,2,3' instead of sending separate fields altogether place '1' place '2' place '3' . By coincidence, it would be desirable not to (before)-rework the validator on the server, but to bring the client to the "correct" state. Place parameters are always a very different number. There were options to use MultiMap, but Retrofit does not accept it, because. it does not implement the necessary interfaces (it is generally Google's pojo). An interesting note is that the '@EncodedQuery' annotation gives the desired effect, but sends data not in the POST body, but through the query string. Any suggestions for a solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Pavlov, 2014-11-27
@gurinderu

So your Retrofit does everything right. In this form, List should rely without problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question