arturobernalg commented on code in PR #681: URL: https://github.com/apache/httpcomponents-client/pull/681#discussion_r2240488876
########## httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientDeflateCompressionExample.java: ########## @@ -0,0 +1,106 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.hc.client5.http.examples; + +import java.util.concurrent.Future; + +import org.apache.hc.client5.http.async.methods.DeflatingAsyncEntityProducer; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder; +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; +import org.apache.hc.client5.http.impl.async.HttpAsyncClients; +import org.apache.hc.core5.concurrent.FutureCallback; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.Message; +import org.apache.hc.core5.http.nio.AsyncEntityProducer; +import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer; +import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer; +import org.apache.hc.core5.http.nio.support.BasicRequestProducer; +import org.apache.hc.core5.http.nio.support.BasicResponseConsumer; + +/** + * Demonstrates how to POST a JSON body compressed on-the-fly with + * {@link DeflatingAsyncEntityProducer}. The program talks to + * <a href="https://httpbin.org/post">httpbin</a>, which echoes your request, + * making it easy to verify that the {@code Content-Encoding: deflate} + * header was honoured. + * + * <p>Key take-aways:</p> + * <ol> + * <li>Wrap any existing {@code AsyncEntityProducer} in the compressor.</li> + * <li>Add the encoding header yourself – HttpClient does not do that for you.</li> + * <li>The producer is streaming: huge payloads are never fully buffered.</li> + * </ol> + * + * @since 5.6 + */ +public class AsyncClientDeflateCompressionExample { + + public static void main(final String[] args) throws Exception { + try (final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault()) { + client.start(); + + final String json = "{\"msg\":\"hello deflated world\"}"; + final AsyncEntityProducer raw = + new StringAsyncEntityProducer(json, ContentType.APPLICATION_JSON); + + final AsyncEntityProducer deflated = new DeflatingAsyncEntityProducer(raw); + + final SimpleHttpRequest request = SimpleRequestBuilder + .post("https://httpbin.org/post") + .addHeader("Content-Encoding", "deflate") Review Comment: @ok2c you are right. Is not need it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For additional commands, e-mail: dev-h...@hc.apache.org