Continuing from our last blog, we will add a POST method to the API and consume it from React. We will keep track of the added comments by using the
Let’s extend the API type with a POST method :
= "comments" :> Get '[ JSON] (ListHeaders [Comment])
:<|> "comments" :> Capture "id" Int :> Get '[ JSON] Comment
:<|> "comments" :> ReqBody '[ JSON] NewComment :> Post '[JSON] NoContent
Our last blog ended with a very simple REST server and a UI that could list comments, and that was it. Let’s expand it a bit with GET requests to fetch single comments!
The GET request itself is reasonably close to what we already have. Let’s recall our current type :
type WebApi = "comments" :> Get '[ JSON] (ListHeaders [Comment])
We will add a second REST route first of all. Secondly, a GET request must specify one single
comment it wants to retrieve so it needs some kind of unique id. …
In the first episode of our blogs we ended by having a working Haskell REST server serving our Servant WebApi type. The use case we are working towards is serving comments in a web app. A comment is currently just a string of text but we can expand on it in the future. In this part we will try to write a UI for it, exploring some React Admin specifics as we go.
React Admin is a React framework that allows you to scaffold a CRUD web UI fast and easy as long as you stick to the defaults it…
I prefer using Haskell for my day-to-day programming challenges: the expressiveness and the type system make me a lot more productive. A typical project consists of a back-end and front-end part, and while Haskell has a clear track record for backend work, its application to frontend is far less common. There are of course front end frameworks in Haskell, but it is usually difficult to convince front end devs or designers to learn the shakespearean templating languages from Yesod for instance.