-1

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();
    }
}

2 Answers2

4

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.

perovic
  • 246
  • 1
  • 7
0

It is not a limit defined by your HTTP client (okHttp), it is a server limitation.

Basically, the server that you are requesting does not accept large request bodies. I don't know if it is a public API or not but you should check their documentation to see what is wrong with your request or if you are sure that you implemented correctly you should contact the host.

oldborn
  • 66
  • 2