Retrofit2: Modifying request body in OkHttp Interceptor

前端 未结 4 1896
遥遥无期
遥遥无期 2020-12-01 15:37

I am using Retrofit 2 (2.0.0-beta3) with OkHttp client in Android application and so far everything going great. But currently I am facing issue with OkHttp Interceptor. The

相关标签:
4条回答
  • 2020-12-01 16:19

    I'm using this way to verify my token

    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(30, TimeUnit.SECONDS) //retrofit default 10 seconds
                    .writeTimeout(30, TimeUnit.SECONDS) //retrofit default 10 seconds
                    .readTimeout(30, TimeUnit.SECONDS) //retrofit default 10 seconds
                    .addInterceptor(logging.setLevel(HttpLoggingInterceptor.Level.BODY))
                    .addInterceptor(new BasicAuthInterceptor())
                    .build();
    

    Here i'm sending token through BasicAuthInterceptor

    public class MyServiceInterceptor implements Interceptor {
    
    private String HEADER_NAME="Authorization";
    private String OBJECT_NAME="Bearer";
    private String SPACE="  ";
    @Override public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
    
        Request.Builder requestBuilder = request.newBuilder();
    
        String token= PreferenceManager.getInstance().getString(PreferenceManager.TOKEN);
            if (token != null) { {
                requestBuilder.addHeader(HEADER_NAME, OBJECT_NAME+SPACE+ token);
            }
    }
    
        return chain.proceed(requestBuilder.build());
    

    } }

    0 讨论(0)
  • 2020-12-01 16:22

    I using this to add post parameter to the existing ones.

     OkHttpClient client = new OkHttpClient.Builder()
                        .protocols(protocols)
                        .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                Request request = chain.request();
                                Request.Builder requestBuilder = request.newBuilder();
    RequestBody formBody = new FormEncodingBuilder()
                .add("email", "Jurassic@Park.com")
                .add("tel", "90301171XX")
                .build();
                                String postBodyString = Utils.bodyToString(request.body());
                                postBodyString += ((postBodyString.length() > 0) ? "&" : "") +  Utils.bodyToString(formBody);
                                request = requestBuilder
                                        .post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), postBodyString))
                                        .build();
                                return chain.proceed(request);
                            }
                        })
                        .build();
    
    public static String bodyToString(final RequestBody request){
            try {
                final RequestBody copy = request;
                final Buffer buffer = new Buffer();
                if(copy != null)
                    copy.writeTo(buffer);
                else
                    return "";
                return buffer.readUtf8();
            }
            catch (final IOException e) {
                return "did not work";
            }
        }
    

    OkHttp3:

    RequestBody formBody = new FormBody.Builder()
                    .add("email", "Jurassic@Park.com")
                    .add("tel", "90301171XX")
                    .build();
    
    0 讨论(0)
  • 2020-12-01 16:23

    I'll share my Kotlin implementation of @Fabian's answer using Dagger. I wanted origin=app added to the request url for GET requests, and added to the body for form-encoded POST requests

    @Provides
    @Singleton
    fun providesRequestInterceptor() =
            Interceptor {
                val request = it.request()
    
                it.proceed(when (request.method()) {
                    "GET" -> {
                        val url = request.url()
                        request.newBuilder()
                                .url(url.newBuilder()
                                        .addQueryParameter("origin", "app")
                                        .build())
                                .build()
                    }
                    "POST" -> {
                        val body = request.body()
                        request.newBuilder()
                                .post(RequestBody.create(body?.contentType(),
                                        body.bodyToString() + "&origin=app"))
                                .build()
                    }
                    else -> request
                })
            }
    
    fun RequestBody?.bodyToString(): String {
        if (this == null) return ""
        val buffer = okio.Buffer()
        writeTo(buffer)
        return buffer.readUtf8()
    }
    
    0 讨论(0)
  • 2020-12-01 16:31

    Since this cannot be written in the comments of the previous answer by @Fabian, I am posting this one as separate answer. This answer deals with both "application/json" as well as form data.

    import android.content.Context;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.io.IOException;
    
    import okhttp3.FormBody;
    import okhttp3.Interceptor;
    import okhttp3.MediaType;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
    import okio.Buffer;
    
    /**
     * Created by debanjan on 16/4/17.
     */
    
    public class TokenInterceptor implements Interceptor {
        private Context context; //This is here because I needed it for some other cause 
    
        //private static final String TOKEN_IDENTIFIER = "token_id";
        public TokenInterceptor(Context context) {
            this.context = context;
        }
    
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            RequestBody requestBody = request.body();
            String token = "toku";//whatever or however you get it.
            String subtype = requestBody.contentType().subtype();
            if(subtype.contains("json")){
                requestBody = processApplicationJsonRequestBody(requestBody, token);
            }
            else if(subtype.contains("form")){
                requestBody = processFormDataRequestBody(requestBody, token);
            }
            if(requestBody != null) {
                Request.Builder requestBuilder = request.newBuilder();
                request = requestBuilder
                        .post(requestBody)
                        .build();
            }
    
            return chain.proceed(request);
        }
        private String bodyToString(final RequestBody request){
            try {
                final RequestBody copy = request;
                final Buffer buffer = new Buffer();
                if(copy != null)
                    copy.writeTo(buffer);
                else
                    return "";
                return buffer.readUtf8();
            }
            catch (final IOException e) {
                return "did not work";
            }
        }
        private RequestBody processApplicationJsonRequestBody(RequestBody requestBody,String token){
            String customReq = bodyToString(requestBody);
            try {
                JSONObject obj = new JSONObject(customReq);
                obj.put("token", token);
                return RequestBody.create(requestBody.contentType(), obj.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        private RequestBody processFormDataRequestBody(RequestBody requestBody, String token){
            RequestBody formBody = new FormBody.Builder()
                    .add("token", token)
                    .build();
            String postBodyString = bodyToString(requestBody);
            postBodyString += ((postBodyString.length() > 0) ? "&" : "") +  bodyToString(formBody);
            return RequestBody.create(requestBody.contentType(), postBodyString);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题