1

I am using Retrofit to send request as encrypted JWT (JWE) to an API.

My service interface is:

interface APICallService {

    @Headers("Content-Type: application/jwt")
    @POST("/v1/api/dp_checkkyc")
    fun getKycCompliantStatus(@Header("Authorization") accessToken:String,  kycStatusRequest: KycStatusRequest): Call<KycCompliantBaseResponse>

}

My KycStatusRequest class is:

data class KycStatusRequest(var encryptedJWT : String)

I am hitting the API with:

fun getEKycCompliantStatus(accessToken:String, pan:String) {
            var jwe = EncryptedJWTGenerator(pan).jweString //This JWE works fine with Postman
            val kycStatusRequest = KycStatusRequest(jwe)
            val call = getServiceInstance().getKycCompliantStatus("Bearer ${accessToken.trim()}", kycStatusRequest)
            call.enqueue(object : Callback<KycCompliantBaseResponse> {
                override fun onResponse(call: Call<KycCompliantBaseResponse>, response: Response<KycCompliantBaseResponse>) {
                    if (response.code() == 200) {
                        val kycResponse = response.body()!!
                        if (kycResponse.Response.F_PAN_STATUS.equals("ok", true))
                            isKycCompliant = true

                        else if (kycResponse.Response.F_PAN_STATUS.equals("invalid", true))
                            isKycCompliant = false

                    }

                    else
                        Toast.makeText(context,"Check kyc API failure!", Toast.LENGTH_LONG).show()    
                }

                override fun onFailure(call: Call<KycCompliantBaseResponse>, t: Throwable) {
                    Toast.makeText(context,"Check kyc API failure!", Toast.LENGTH_LONG).show()
                }
            })
        }

On using the above code I get 'Internal Server Error'. But on using the same jwe I used above with postman, API works fine.

I am suspecting that I am getting this error as I am wrapping my JWE in KycStatusRequest class before sending, which I think will convert it into a JSON with key-value pair.

How do I send my JWE as a raw text without any key-value pair?

Sparsh Dutta
  • 2,450
  • 4
  • 27
  • 54

2 Answers2

0

Solved by wrapping up my JWE with RequestBody class as:

val requestBody: RequestBody = RequestBody.create(MediaType.parse("text/plain"), jwe)
Sparsh Dutta
  • 2,450
  • 4
  • 27
  • 54
0

I think you'll need to add a converter Factory to your retrofit builder, like this one:

private val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create()) //Converters can be added to support other types in body
            .build()

You can learn more about converterfactory and retrofit on its website: https://square.github.io/retrofit/

ladytoky0
  • 588
  • 6
  • 16