How to create a repeatable POST request containing multipart data?

I am trying to create a POST request containing multipart-form-data that requires NT Credentials . The authentication request causes the POST be retried, and I get an exception of an exception object.

I tried to wrap a MultipartContent object created using BufferedHttpEntity , but does it throw NullPointerExceptions ?

 final GenericUrl sau = new GenericUrl(baseURI.resolve("Record")); final MultipartContent c = new MultipartContent().setMediaType(MULTIPART_FORM_DATA).setBoundary("__END_OF_PART__"); final MultipartContent.Part p0 = new MultipartContent.Part(new HttpHeaders().set("Content-Disposition", format("form-data; name=\"%s\"", "RecordRecordType")), ByteArrayContent.fromString(null, "C_APP_BOX")); final MultipartContent.Part p1 = new MultipartContent.Part(new HttpHeaders().set("Content-Disposition", format("form-data; name=\"%s\"", "RecordTitle")), ByteArrayContent.fromString(null, "JAVA_TEST")); c.addPart(p0); c.addPart(p1); 

The documentation for ByteArrayContent says

A specific implementation of AbstractInputStreamContent that generates repeatable input streams based on the contents of a byte array.

Performing all parts repeatedly does not solve the problem. Because this code is System.out.println("c.retrySupported() = " + c.retrySupported()); outputs c.retrySupported() = true .

I found the following documentation:

1.1.4.1. Duplicate objects An entity can be repeatable, that is, its contents can be read more than once. This is only possible with (e.g. ByteArrayEntity or StringEntity)

Now I converted my MultipartContent to ByteArrayContent with the media type multi/part-form , extracting the contents of the string and still get the same error!

But I still get the following exception when I try to call request.execute() .

 Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity. 

So how can I convince ApacheHttpTransport to create a repeatable Entity?

+7
source share
2 answers

I had to change all the classes inherited from HttpContent so that they would correctly report using .retrySupported() so that when I entered the ApacheHttpTransport code, ApacheHttpTransport would generate reproducible content correctly.

Changes were made with respect to version 1.20.0 , because this is what I used. I am sending a detachment request from dev branch HEAD , so hopefully this or some version of this will lead to the next version.

The following are the changes that need to be combined.

+5
source share

If the length of the contents of all parts in a multi-part entity is known (returned as a non-negative value), the object will be considered as repeatable. The easiest way to repeat the repeatability of multi-part objects is to make all its parts repeatable.

0
source share

All Articles