Search code examples
kotlinretrofit

Kotlin Retrofit, I made a post request with a long string but when I checked the result in the server is full of \n characters


I'm using Retrofit to send data to a database but the body saved not is the same that I sent

I'm sending One image in base64 but when I check the file I see that is full of /n spaces

What could be happened?

the problem is with the variable called Adjunto

Codes:

Retrofit provider:

@Singleton
@Provides
fun provideRetrofit(): Retrofit {
    return Retrofit.Builder()
        .addConverterFactory(MoshiConverterFactory.create())
        .baseUrl(Constans.BASE_URL)
        .build()
}

I had used Gson Converter but it doesn't work

Retrofit Api:

@POST("RS_Documentos")
suspend fun newDocument (@Body document: NewDocument): Response<DocumentResponse>

Body Sent:

    override suspend fun newDocument(document: DocumentsItem): Resource<DocumentResponse> {

    try {
        
        var newDocument = NewDocument(
            Adjunto = document.Adjunto,
            Apellido = document.Apellido,
            Ciudad = document.Ciudad,
            Correo = document.Correo,
            Identificacion = document.Identificacion,
            Nombre = document.Nombre,
            TipoAdjunto = document.TipoAdjunto,
            TipoId = document.TipoId
        )

        var response = iDocumentDAO.newDocument(document = newDocument).body()
       
        return Resource.Success(data = response!!)

    } catch (e: Exception) {
        return Resource.Error("The Document doesn't was created")

    }
    return Resource.Error("The Document doesn't was created")
}

}

Now a part of the base64 images:

base 64 sent

But when I take a look at the picture in the database is seem in this way: Base 64 Saved


Solution

  • I have found the solution.

    The problem was in the body sent IDK why the val adjunto had \n spaces but when I cleared the val adjunto of whitespace and newlines the file was sent correctly.

    This is the new code:

        override suspend fun newDocument(document: DocumentsItem): Resource<DocumentResponse> {
    
        try {
            Log.d("Document email", document.Correo.toString())
            val result= document.Adjunto!!.replace("\n","")
            val newDocument = NewDocument(
                adjunto = result,
                apellido = document.Apellido!!,
                ciudad = document.Ciudad!!,
                correo = document.Correo!!,
                identificacion = document.Identificacion!!,
                nombre = document.Nombre!!,
                tipoAdjunto = document.TipoAdjunto!!,
                tipoId = document.TipoId!!
            )
            Log.d("Document Created", newDocument.toString())
    
            val response = iDocumentDAO.newDocument(document = newDocument).body()
            Log.d("Document Created", response.toString())
            return Resource.Success(data = response!!)
    
        } catch (e: Exception) {
            return Resource.Error("The Document doesn't was created")
    
        }
    }