Created
February 5, 2018 06:06
-
-
Save shokimble/ff969668844399321a049c7e0f1351da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.facebook.react.modules.network; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import java.io.IOException; | |
import okhttp3.Interceptor; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
public class NetworkingModuleUtils { | |
public static NetworkingModule createNetworkingModuleWithCustomClient(ReactApplicationContext context) { | |
OkHttpClient client = OkHttpClientProvider.createClient(); | |
OkHttpClient customClient = client.newBuilder() | |
.addNetworkInterceptor(new Interceptor() { | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
String contentType = request.header("Content-Type"); | |
Request customRequest = "application/json; charset=utf-8".equals(contentType) ? | |
request.newBuilder() | |
.header("content-type", "application/json") | |
.build() : request; | |
Response response = chain.proceed(customRequest); | |
return response; | |
} | |
}).build(); | |
return new NetworkingModule(context, null, customClient); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment