I’m trying to do a redirect from an endpoint handler and so far am not having much luck doing it. Here’s my use case.
The app is a flashcards app for memorizing various information. There are, among others, two endpoints: `POST /decks`, to create a new deck of cards, and `GET /card_type/{id}/edit`, to edit a card type.
`/decks` creates a new deck and also a new default card type. This is done from the `handleSuccess` method. Now, at the end, I want the user to be redirected to the `/card_type/{id}/edit`, using the `{id}` created by the `/decks` handler. Here’s the code:
val createDeckEndpoint = endpoint
.post.in("decks").in(formBody[Seq[(String, String)]]).out(stringBody)
.out(header(Header.contentType(MediaType.TextHtml)))
val serverCreateDeckEndpoint = createDeckEndpoint
.handleSuccess:
(body: Seq[(String, String)]) =>
val deck = /* Some logic to build the deck model */
val deckId = service.decks.createDeck(deck)
// Create the first card type
val cardType = /* Some logic to build the CardType model */
val cardTypeId = service.cardTypes.createCardType(cardType)
// Redirect the user to the edit page for the newly created card type: /card_type/{cardTypeId}/edit
Does anyone have an idea how can I accomplish such a redirect?
The server logic produces values as specified by the endpoint description, and maps them to headers/body. If you want to do conditional redirects, simply include an optional header in the output description:
Here I have an endpoint that is supposed to be accepting both AJAX and ordinary requests. In case of AJAX requests, I need to push a corresponding URL to the browser history, with the custom `Headers.hxPushUrl` header.
Is there a way to transfer the `in` parameter `path[Long]` into the `out` header’s `cardTypeId` variable? Note that in this case, I also emit the HTML body, so the trick with returning the header value from the body won’t work…