Prevent retrofit from encoding my http request body

后端 未结 4 1536
刺人心
刺人心 2021-01-11 11:30

I\'m trying to pass a string of the format below as the body of a http post request.

param1=PARAM1¶m2=PARAM2¶m3=PARAM3

But

4条回答
  •  有刺的猬
    2021-01-11 12:11

    Using Kotlin

    For Retrofit 2 you can initialize retrofit with a Gson converter factory.

    val builder = GsonBuilder().disableHtmlEscaping().create()
    val retrofit = Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(builder))
            .client(monoOkHttpClient())
            .build()
    

    This builder should remove escaping from your json output.

    Gradle file dependencies:

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    

提交回复
热议问题