What is the proper way to make a post request? Several people have asked this in the past, but the answer still seems unclear. I want a SideNav menu item that starts a Stripe checkout session. I could change it to a GET request, but a POST request is more appropriate.
I suppose I could just inject an HTML form, but I suspect there is a Vaadin way to do this.
private SideNav createNavigation() {
SideNav nav = new SideNav();
List<MenuEntry> menuEntries = MenuConfiguration.getMenuEntries();
menuEntries.forEach(entry -> {
if (entry.icon() != null) {
nav.addItem(new SideNavItem(entry.title(), entry.path(), new SvgIcon(entry.icon())));
} else {
nav.addItem(new SideNavItem(entry.title(), entry.path()));
}
});
SideNavItem addCreditNavItem = new SideNavItem("Add Credit", "create-checkout-session");
nav.addItem(addCreditNavItem);
return nav;
}
@Controller
@RequestMapping("/create-checkout-session")
public class CreateCheckoutSessionController {
private final CheckoutSessionService checkoutSessionService;
public CreateCheckoutSessionController(CheckoutSessionService checkoutSessionService) {
this.checkoutSessionService = checkoutSessionService;
}
@PostMapping
public RedirectView createCheckoutSession() {
try {
String sessionUrl = checkoutSessionService.createCheckoutSession();
return new RedirectView(sessionUrl);
} catch (Exception e) {
Broadcaster.broadcast("Failed to create a Stripe session: " + e.getMessage());
return new RedirectView("");
}
}
}
I think your way is fine (although it might need a security review). Vaadin the framework abstracts the network communication layer away from the application developer, so there’s no correct “Vaadin” way to send a specific type of request. In my opinion, it’s best to treat any integrations like that as fully separate applications.
Thanks! And, I don’t understand. I did look at event.forwardTo() and event.forwardToUrl, but I couldn’t find any information to explain whether those were better or more appropriate for my use case.
Methods used on the event are passed to the “Navigation lifecircle” - which let’s Vaadin properly (as designed) handle additional navigations / redirect within the current navigation. With UI::getCurrent… you are bypassing the “official” way by creating a completely new navigation - imho it feels “dirty”