我正在使用Retrofit将请求作为加密的JWT (JWE)发送到API。
我的服务接口是:
interface APICallService {
@Headers("Content-Type: application/jwt")
@POST("/v1/api/dp_checkkyc")
fun getKycCompliantStatus(@Header("Authorization") accessToken:String, kycStatusRequest: KycStatusRequest): Call<KycCompliantBaseResponse>
}我的KycStatusRequest类是:
data class KycStatusRequest(var encryptedJWT : String)我正在用下面的代码来敲击API:
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()
}
})
}在使用上面的代码时,我得到了'Internal Server Error'。但在使用我上面对postman使用的相同jwe时,API工作得很好。
我怀疑我收到了这个错误,因为我在发送之前将JWE包装在KycStatusRequest类中,我认为这会将它转换为具有键-值对的JSON。
如何在没有任何键值对的情况下将JWE作为原始文本发送?
发布于 2021-07-28 18:29:15
通过使用RequestBody类包装我的JWE来解决:
val requestBody: RequestBody = RequestBody.create(MediaType.parse("text/plain"), jwe)发布于 2021-07-29 12:58:42
我认为你需要在你的改装构建器中添加一个转换器工厂,就像下面这样:
private val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create()) //Converters can be added to support other types in body
.build()您可以在其网站上了解更多关于converterfactory和翻新的信息:https://square.github.io/retrofit/
https://stackoverflow.com/questions/68546085
复制相似问题