Foso / Ktorfit

HTTP client generator / KSP plugin for Kotlin Multiplatform (Android, iOS, Js, Jvm, Native, WasmJs)) using KSP and Ktor clients inspired by Retrofit https://foso.github.io/Ktorfit
https://foso.github.io/Ktorfit
Apache License 2.0
1.61k stars 43 forks source link

Append Headers Not Working #597

Closed chen-lihan closed 5 months ago

chen-lihan commented 5 months ago

Ktorfit version

2.0.0

What happened and how can we reproduce this issue?

Hello, sorry to bother you I am developing a KMP project and using Ktorfit as my networking tool. When uploading files, I noticed that the headers are not being added. I am not sure why this is happening, but using Ktor's submitFormWithBinaryData for file uploads works successfully. Could you please take a look?

API

@Multipart
@POST("mails/send")
suspend fun sendMessageFile(
        @Part("client_msg_id") id: Int,
        @Part("company_id") company_id: Int,
        @Part("") attached_files: List<PartData>
): Response<Unit>

Ktorfit

val multipart = formData{
    append(key = "attached_files[]" , value = dataBytes , Headers.build {
      append(HttpHeaders.ContentDisposition, "filename=${attachedFiles[0].name}")
       } 
   )
}

apiMessage.sendMessageFile(
   id = message.clientMsgId ?: 0,
   company_id = message.companyId ?: 0,
   attached_files = multipart
 )

Ktor

client.submitFormWithBinaryData(
     url = apiKey + "mails/send",
      formData = formData {
             append("client_msg_id", message.clientMsgId?:0)
             append("company_id", message.companyId?:0)
             append("attached_files[]", data, Headers.build {
                   append(HttpHeaders.ContentDisposition, "filename=${attachedFiles[0].name}")
         })
       }
  )

Log: ---28623c11-520381fc-52c6673d2198767a17754b66-418595641f2bf0b7e70fa8b-40 Content-Disposition: form-data; name=client_msg_id Content-Length: 9 ---28623c11-520381fc-52c6673d2198767a17754b66-418595641f2bf0b7e70fa8b-40 Content-Disposition: form-data; name=company_id Content-Length: 2 ---28623c11-520381fc-52c6673d2198767a17754b66-418595641f2bf0b7e70fa8b-40 Content-Disposition: form-data; name="" Content-Length: 47

io.ktor.http.content.PartData$BinaryItem@a06930 ---28623c11-520381fc-52c6673d2198767a17754b66-418595641f2bf0b7e70fa8b-40--

What did you expect to happen?

This can work , i wish

val multipart = formData{
                append(key = "attached_files[]" , value = data , Headers.build {
                    append(HttpHeaders.ContentDisposition, "filename=${attachedFiles[0].name}")
                 }
                )
            }

Is there anything else we need to know about?

No response

chen-lihan commented 5 months ago

Sorry My mistake..