private void handleAddProduct(RoutingContext routingContext) { String productID = routingContext.request().getParam("productID"); HttpServerResponse response = routingContext.response(); if (productID == null) { sendError(400, response); } else { JsonObject product = routingContext.getBodyAsJson(); if (product == null) { sendError(400, response); } else { products.put(productID, product); response.end(); } } }
private void handleAddProduct(RoutingContext routingContext) { String productID = routingContext.request().getParam("productID"); HttpServerResponse response = routingContext.response(); if (productID == null) { sendError(400, response); } else { JsonObject product = routingContext.getBodyAsJson(); if (product == null) { sendError(400, response); } else { products.put(productID, product); response.end(); } } }
private void handleAddProduct(RoutingContext routingContext) { HttpServerResponse response = routingContext.response(); SQLConnection conn = routingContext.get("conn"); JsonObject product = routingContext.getBodyAsJson(); conn.updateWithParams("INSERT INTO products (name, price, weight) VALUES (?, ?, ?)", new JsonArray().add(product.getString("name")).add(product.getFloat("price")).add(product.getInteger("weight")), query -> { if (query.failed()) { sendError(500, response); } else { response.end(); } }); }
JsonObject newUser = ctx.getBodyAsJson(); JsonObject update = ctx.getBodyAsJson();
@Override public JsonObject getBodyAsJson() { return decoratedContext.getBodyAsJson(); }
@Override public JsonObject getBodyAsJson() { return inner.getBodyAsJson(); }
JsonObject credentials = ctx.getBodyAsJson(); if (credentials == null) {
@Test public void test_object_as_json_yields_json_object() throws Exception { router.route().handler(event -> { assertEquals(new JsonObject(Collections.singletonMap("foo", "bar")), event.getBodyAsJson()); event.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.write(Buffer.buffer("{ \"foo\": \"bar\" }")); }, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null); }
@Test public void test_empty_object_as_json_yields_empty_json_object() throws Exception { router.route().handler(event -> { assertEquals(new JsonObject(), event.getBodyAsJson()); event.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.write(Buffer.buffer("{ }")); }, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null); }
@Test public void testBodyJson() throws Exception { JsonObject json = new JsonObject().put("foo", "bar").put("blah", 123); router.route().handler(rc -> { assertEquals(json, rc.getBodyAsJson()); rc.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.write(json.encode()); }, 200, "OK", null); }
@Test public void testBodyJsonWithHugeContentLength() throws Exception { JsonObject json = new JsonObject().put("foo", "bar").put("blah", 123); router.route().handler(rc -> { assertEquals(json, rc.getBodyAsJson()); rc.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(Long.MAX_VALUE)); req.write(json.encode()); }, 200, "OK", null); }
/** * @return Get the entire HTTP request body as a . The context must have first been routed to a {@link io.vertx.rxjava.ext.web.handler.BodyHandler} for this to be populated. */ public JsonObject getBodyAsJson() { JsonObject ret = delegate.getBodyAsJson(); return ret; }
@Test public void testBodyJsonWithNegativeContentLength() throws Exception { JsonObject json = new JsonObject().put("foo", "bar").put("blah", 123); router.route().handler(rc -> { assertEquals(json, rc.getBodyAsJson()); rc.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.putHeader(HttpHeaders.CONTENT_LENGTH, "-1"); req.write(json.encode()); }, 200, "OK", null); }
@Test public void testBodyJsonWithEmptyContentLength() throws Exception { JsonObject json = new JsonObject().put("foo", "bar").put("blah", 123); router.route().handler(rc -> { assertEquals(json, rc.getBodyAsJson()); rc.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.putHeader(HttpHeaders.CONTENT_LENGTH, ""); req.write(json.encode()); }, 200, "OK", null); }
@Override public JsonObject getBodyAsJson() { return inner.getBodyAsJson(); }
@Test public void test_remove_data() throws Exception { router.route().handler(event -> { String foo = event.getBodyAsJson().encode(); event.put("foo", foo); String removedFoo = event.remove("foo"); assertEquals(removedFoo, foo); assertNull(event.get("foo")); event.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.write(Buffer.buffer("{ \"foo\": \"bar\" }")); }, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null); }
private void apiAddCartEvent(RoutingContext context, JsonObject principal) { String userId = Optional.ofNullable(principal.getString("userId")) .orElse(TEST_USER); CartEvent cartEvent = new CartEvent(context.getBodyAsJson()); if (validateEvent(cartEvent, userId)) { shoppingCartService.addCartEvent(cartEvent, resultVoidHandler(context, 201)); } else { context.fail(400); } }
private void apiAddUser(RoutingContext context) { Account account = new Account(context.getBodyAsJson()); accountService.addAccount(account, resultVoidHandler(context, 201)); }
private void doRegisterDeviceJson(final RoutingContext ctx) { try { JsonObject payload = null; if (ctx.getBody().length() > 0) { payload = ctx.getBodyAsJson(); } registerDevice(ctx, payload); } catch (final DecodeException e) { HttpUtils.badRequest(ctx, "body does not contain a valid JSON object"); } }
@Test public void test_empty_object_as_json_yields_empty_json_object() throws Exception { router.route().handler(event -> { assertEquals(new JsonObject(), event.getBodyAsJson()); event.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.write(Buffer.buffer("{ }")); }, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null); }