Search code examples
javarequestokhttphttp-status-code-413

okhttp3 - 413 Request Entity Too Large


When my system send a http request using okhttp3 the follow issue occurs: 413 Request Entity Too Large

I would to know how I set a maximum size in okhttp3 request or if there is another solution for this issue.

public static String post(String url, String json) throws IOException {
    OkHttpClient client = new OkHttpClient().newBuilder()
            .readTimeout(1, TimeUnit.MINUTES)
            .build();
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
    .header("Authorization", authKey)
    .url(url)
    .post(body)
    .build();
    try (Response response = client.newCall(request).execute()) {
        if(response.code() != 200) {
            return "request error";
        }
        return response.body().string();
    }
}

Solution

  • This is not actually an issue with your code, rather the server you are sending the data to has a limitation on the request payload size. For more info about the error see: HTTP Response Status Code 413: Payload too large

    If you are in control of the server you may adjust this setting. Examples for fixing this error for a couple of common servers (Apache, NGINX, IIS) may be found here.

    If you are not in control, you may want to look at the API / documentation of the service you are using for other ways of uploading the data. The service might simply have strict limitations on payload size for that particular use-case.