Search in sources :

Example 1 with StringPart

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);
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) StringPart(org.asynchttpclient.request.body.multipart.StringPart) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 2 with StringPart

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();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FileLikePart(org.asynchttpclient.request.body.multipart.FileLikePart) Part(org.asynchttpclient.request.body.multipart.Part) StringPart(org.asynchttpclient.request.body.multipart.StringPart) MultipartBody(org.asynchttpclient.request.body.multipart.MultipartBody) ArrayList(java.util.ArrayList) StringPart(org.asynchttpclient.request.body.multipart.StringPart) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Aggregations

StringPart (org.asynchttpclient.request.body.multipart.StringPart)2 Test (org.testng.annotations.Test)2 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 ArrayList (java.util.ArrayList)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 FileLikePart (org.asynchttpclient.request.body.multipart.FileLikePart)1 MultipartBody (org.asynchttpclient.request.body.multipart.MultipartBody)1 Part (org.asynchttpclient.request.body.multipart.Part)1 AsyncCompletionHandlerAdapter (org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter)1 HttpTest (org.asynchttpclient.testserver.HttpTest)1