Home > Software engineering >  HTTP Error 411 for startOptimization request of routeoptimization api
HTTP Error 411 for startOptimization request of routeoptimization api

Time:08-18

I'm trying to send a startOptimization request to https://api.myptv.com/routeoptimization. I generated my java client as described in the tutorial: https://developer.myptv.com/Tutorials/General/clientGeneration.htm

Creating a plan works but when I want to optimize it I get the following error:

HTTP Error 411. The request must be chunked or have a content length.

I noticed that this error is also returned when I send a startEvaluation request. All other requests of the route optimization api and all other APIs seems to work.

CodePudding user response:

Unfortunately this is a bug in the API Managment of Microsoft. We created a ticket for Microsoft but we don't know when it is fixed. The problem is that the API Managment rejects POST requests with an empty body where no Content-Length header of 0 is passed. The java client does not pass this header for an empty body, therefore either the empty body must be avoided or the header must be added until the bug is fixed.

There are two possible solution to solve this problem:

  1. Just pass a dummy body which is ignored anyway. For this you have to adapt the java client after generation: replace all occurencies of
localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());

with

try {
  byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes("dummybody");
  localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
} catch (IOException e) {
  throw new ApiException(e);
}
  1. Explicitly send a Content-Length header of 0 by adding
System.setProperty("jdk.httpclient.allowRestrictedHeaders", "Content-Length");

to a setup method of your main and adding

localVarRequestBuilder.header("Content-Length", "0");

to the startEvaluationRequestBuilder and startOptimizationRequestBuilder methods.

  • Related