title: Android Retrofit upload images and text in the same request
body: <p>I have an Android app that has a form feature where I collect data, that data contains image answers and text answers and each answer has a question id that I need to send also to know which question to save answer to it in the backend API, I tried to send all answers both images and text in the same request using Retrofit but got some errors, so I thought in another solution, but it's not working anymore here is my code</p>
  <p>Here is my solution and I need to improve it</p>
  <p>Retrofit code to send a request with data</p>
  <pre><code>@Multipart
  @POST(SUBMIT_FORM)
  suspend fun submitFormAPI(
          @Header(&quot;Authorization&quot;) token: String,
          @Part(&quot;questions_answers&quot;) questions_answers: List&lt;QuestionAnswer&gt;,
          @Part answer_image: MultipartBody.Part?,
          @Part(&quot;question_id&quot;) question_id: String?,
  ): Response&lt;FormResponse&gt;
  </code></pre>
  <p>and here is the QuestionAnswer Model, I tried to put answer_image inside it but doesn't work</p>
  <pre><code>data class QuestionAnswer(
      val question_id: String,
      val answer_value: String? = null,
  )
  </code></pre>
  <p>so in this function, after collecting the data I need to loop through questionImagesAnswers and send a request with the image and its question_id every iteration along with other answers in questionAnswers</p>
  <pre><code>    fun onFormSubmit() {
  
          val questionAnswers = mutableListOf&lt;QuestionAnswer&gt;()
          val questionImagesAnswers = mutableListOf&lt;QuestionModel&gt;()
  
          questionImagesAnswers.forEach { questionImagesAnswer -&gt;
              formViewModel.submitFormAPI(
                  token = token,
                  questionAnswers = questionAnswers,
                  answer_image = questionImagesAnswer.answer_image_file,
                  question_id = questionImagesAnswer.question_id,
              )
          }
      }
  
  </code></pre>
  <p>What I need if there is a way that helps me put the answer_image in QuestionAnswer with its question id to look like that</p>
  <pre><code>data class QuestionAnswer(
      val question_id: String,
      val answer_value: String? = null,
      val answer_image: MultipartBody.Part?
  )
  </code></pre>
  <p>and then sending data like so without making iterations:</p>
  <pre><code>submitFormAPI(
       token = token,
       questionAnswers = questionAnswers,
  )
  </code></pre>
  <p>that will help me in processing all data at the request in the backend so I could save each answer to its question like so:</p>
  <pre><code>class SubmitFormAPI(views.APIView):
      
      def post(self, request):
          
          questions_answers = request.data['questions_answers']
  
          for question_answer in questions_answers:
              question = Question.objects.filter(question_id=question_answer['question_id']).first()
              if question:
                  if &quot;answer_value&quot; in question_answer:
                      question.answer_value = question_answer['answer_value']
                  if &quot;answer_image&quot; in question_answer:
                      question.answer_image = question_answer['answer_image']
                  question.save()
  
          return Response({
              &quot;status&quot;: status.HTTP_200_OK,
              &quot;message&quot;: &quot;Form submitted successfully!&quot;,
          })
  </code></pre>
  <p>I tried the solution above but doesn't work for me and I don't know why, does anyone know what should I do in such a situation? any help or tips?</p>
  <p>Thanks</p>