use of org.asynchttpclient.request.body.multipart.StringPart in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postSingleStringPart.
@Test
public void postSingleStringPart() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
server.enqueueEcho();
client.preparePost(getTargetUrl()).addBodyPart(new StringPart("foo", "bar")).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
String requestContentType = response.getHeader("X-" + CONTENT_TYPE);
String boundary = requestContentType.substring((requestContentType.indexOf("boundary") + "boundary".length() + 1));
assertTrue(response.getResponseBody().regionMatches(false, "--".length(), boundary, 0, boundary.length()));
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of org.asynchttpclient.request.body.multipart.StringPart in project async-http-client by AsyncHttpClient.
the class MultipartPartTest method transferToShouldWriteStringPart.
@Test
public void transferToShouldWriteStringPart() throws IOException, URISyntaxException {
String text = FileUtils.readFileToString(TestUtils.resourceAsFile("test_sample_message.eml"));
List<Part> parts = new ArrayList<>();
parts.add(new StringPart("test_sample_message.eml", text));
HttpHeaders headers = new DefaultHttpHeaders();
headers.set("Cookie", "open-xchange-public-session-d41d8cd98f00b204e9800998ecf8427e=bfb98150b24f42bd844fc9ef2a9eaad5; open-xchange-secret-TSlq4Cm4nCBnDpBL1Px2A=9a49b76083e34c5ba2ef5c47362313fd; JSESSIONID=6883138728830405130.OX2");
headers.set("Content-Length", "9241");
headers.set("Content-Type", "multipart/form-data; boundary=5gigAKQyqDCVdlZ1fCkeLlEDDauTNoOOEhRnFg");
headers.set("Host", "appsuite.qa.open-xchange.com");
headers.set("Accept", "*/*");
String boundary = "uwyqQolZaSmme019O2kFKvAeHoC14Npp";
List<MultipartPart<? extends Part>> multipartParts = MultipartUtils.generateMultipartParts(parts, boundary.getBytes());
MultipartBody multipartBody = new MultipartBody(multipartParts, "multipart/form-data; boundary=" + boundary, boundary.getBytes());
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(8 * 1024);
try {
multipartBody.transferTo(byteBuf);
byteBuf.toString(StandardCharsets.UTF_8);
} finally {
multipartBody.close();
byteBuf.release();
}
}
Aggregations