• 13

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Retrofit2: Add post parameter into interceptor

Inside my Android kotlin app i'm calling some apis by using retrofit2 like

fun callMyApi(
    @Field("myField") myField: String
): Deferred<MyResponseClass>

Now i need to add some common post params to all my api request (and keep the specific ones for each call, in this case i need to keep "myField"), so i'm using an interceptor:

val requestInterceptor = Interceptor { chain ->
    val newRequest = chain.request()
            .add("common1Key", "common1")
            .add("common2Key", "common2")
            .add("common3Key", "common3")

    return@Interceptor chain.proceed(newRequest)

But this implementation fails because the interceptor seems to overwrite myField. How can i fix it?

We can create Interceptor by using two or more common query parameter.

val requestInterceptor = Interceptor { chain ->

            val url = chain.request()
                .addQueryParameter("common1key", "common1")
                .addQueryParameter("common2key", "common2")
                .addQueryParameter("common3key", "common3")
            val request = chain.request()

            return@Interceptor chain.proceed(request)
  • 1
Reply Report

I have added Interceptor for post form body.

interface PostWebApiService {  

fun savePost(
    @Field("title") title: String
): Deferred<Post>

companion object {
    operator fun invoke(): PostWebApiService {
        val requestInterceptor = Interceptor { chain ->
            var request = chain.request()

            val requestBuilder = request.newBuilder()
            val formBody = FormBody.Builder()
                .add("body", "Body")
                .add("userId", "12")
            var postBodyString = bodyToString(request.body())
            val concat = if (postBodyString.isNotEmpty()) "&" else ""
            postBodyString = postBodyString + concat + bodyToString(formBody)
            request = requestBuilder.post(
            return@Interceptor chain.proceed(request)
        val okHttpClient = OkHttpClient.Builder()
        return Retrofit.Builder()

    fun bodyToString(request: RequestBody?): String {
        try {
            var buffer = Buffer()
            return buffer.readUtf8()
        } catch (e: IOException) {
            return "error"
  • 0
Reply Report