Issues converting OkHttp request to Retrofit for translation API

I’m having trouble converting my working OkHttp code to use Retrofit instead. I have a working OkHttp implementation that calls a translation service, but when I try to recreate the same request using Retrofit, it doesn’t work properly.

Here’s my current OkHttp code that works fine:

val httpClient = OkHttpClient()

val contentType = MediaType.parse("application/x-www-form-urlencoded")
val requestBody = RequestBody.create(contentType, "text=Good%20morning&lang=fr")
val httpRequest = Request.Builder()
    .url("https://translate-service.p.rapidapi.com/v2/translate")
    .post(requestBody)
    .addHeader("content-type", "application/x-www-form-urlencoded")
    .addHeader("Accept-Encoding", "application/gzip")
    .addHeader("X-RapidAPI-Host", "translate-service.p.rapidapi.com")
    .addHeader("X-RapidAPI-Key", MY_API_KEY)
    .build()

val result = httpClient.newCall(httpRequest).execute()

I’m trying to convert this to Retrofit but not sure about the correct approach. Here’s what I have so far:

interface TranslationService {

    @Headers(
        "content-type: application/x-www-form-urlencoded",
        "Accept-Encoding: application/gzip",
        "X-RapidAPI-Host: translate-service.p.rapidapi.com",
        "X-RapidAPI-Key: MY_API_KEY"
    )
    @POST("v2/translate")
    suspend fun performTranslation(
        @Body text: String,
        @Query("lang") language: String,
        @Query("source") sourceLanguage: String
    ): Response<TranslationResponse>
}

What am I doing wrong with the Retrofit setup?

The problem is you’re sending form data but Retrofit’s treating it as raw text. Your OkHttp code uses text=Good%20morning&lang=fr as form body, but @Body sends it as a plain string. You need @FormUrlEncoded on the method and swap @Body for @Field("text"). Also change lang from @Query to @Field("lang") since it’s part of the form data.

You’re mixing form data with query parameters. Your OkHttp code sends text=Good%20morning&lang=fr as form-encoded body data, but your Retrofit interface sends text as a body and lang as a query parameter. Use @FormUrlEncoded on your method and @Field parameters instead of @Body and @Query for proper form encoding. Also, move the API key to an interceptor instead of hardcoding it in headers - it’s cleaner and easier to maintain. I had the same issue migrating from OkHttp to Retrofit and this fixed it immediately.

Your problem is that you’re sending form data but Retrofit isn’t set up for it. Your OkHttp code creates a form-encoded request (text=Good%20morning&lang=fr), but your Retrofit method uses @Body which sends raw content instead of form data. Add @FormUrlEncoded to your method and swap @Body and @Query for @Field annotations. Also, Accept-Encoding should probably be application/json, not application/gzip unless the API specifically wants gzip. I hit the same thing migrating old HTTP clients - you’ve got to match the exact request format from your working implementation.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.