Created
March 13, 2015 19:28
-
-
Save leolux/0bd53a2014793aafc80a to your computer and use it in GitHub Desktop.
Getting the HTTP request body fails in 3.0.0-milestone2 (line 68)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loadbalancer.verticles; | |
import io.vertx.core.AbstractVerticle; | |
import io.vertx.core.http.HttpClient; | |
import io.vertx.core.http.HttpClientOptions; | |
import io.vertx.core.http.HttpClientRequest; | |
import io.vertx.core.http.HttpMethod; | |
import io.vertx.core.http.HttpServer; | |
import io.vertx.core.http.HttpServerOptions; | |
import io.vertx.core.http.HttpServerResponse; | |
import io.vertx.core.logging.Logger; | |
import io.vertx.core.logging.impl.LoggerFactory; | |
import io.vertx.ext.apex.core.Router; | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.util.concurrent.TimeUnit; | |
public class GetRequestBodyExample extends AbstractVerticle { | |
private Logger logger; | |
private static final String RESOURCE_PATH = "/http/path"; | |
public void start() { | |
logger = LoggerFactory.getLogger(GetRequestBodyExample.class); | |
startServer(); | |
vertx.setTimer(2000, id -> { | |
startClient(); | |
}); | |
} | |
private void startClient() { | |
HttpClientOptions options = new HttpClientOptions(); | |
HttpClient client = vertx.createHttpClient(options); | |
try { | |
HttpClientRequest request = client.request(HttpMethod.POST, 999, | |
"localhost", RESOURCE_PATH, response -> { | |
// nothing dodo | |
}); | |
String body = "{anykey:'lorem ipsum'}"; | |
request.putHeader("Content-Type", "application/json"); | |
request.putHeader("Content-Length", | |
String.valueOf(body.getBytes("UTF-8").length)); | |
request.setTimeout(TimeUnit.SECONDS.toMillis(5)); | |
request.write(body, "UTF-8"); | |
request.end(); | |
} catch (UnsupportedEncodingException e) { | |
logger.error(e.getMessage()); | |
} finally { | |
client.close(); | |
} | |
} | |
public void startServer() { | |
HttpServerOptions options = new HttpServerOptions(); | |
options.setPort(999); | |
HttpServer server = vertx.createHttpServer(options); | |
Router router = Router.router(vertx); | |
router.route() | |
.method(HttpMethod.POST) | |
.path(RESOURCE_PATH) | |
.handler( | |
routingContext -> { | |
try { | |
String body = routingContext.getBodyAsString(); | |
if (body == null) { | |
logger.error("Getting the request body failed!!!"); | |
routingContext.response().setStatusCode( | |
HttpURLConnection.HTTP_NOT_FOUND); | |
return; | |
} | |
logger.info("Works as expected"); | |
HttpServerResponse response = routingContext | |
.response(); | |
response.setStatusCode(HttpURLConnection.HTTP_OK); | |
response.putHeader("Content-Length", | |
String.valueOf(0)); | |
} finally { | |
routingContext.response().end(); | |
} | |
}); | |
server.requestHandler(router::accept).listen(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment