Skip to content

Instantly share code, notes, and snippets.

@shokimble
Created February 5, 2018 06:06
Show Gist options
  • Save shokimble/ff969668844399321a049c7e0f1351da to your computer and use it in GitHub Desktop.
Save shokimble/ff969668844399321a049c7e0f1351da to your computer and use it in GitHub Desktop.
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