17 HTTP API with http4s

 

After reading this lesson, you will be able to

  • Run an HTTP server using sbt
  • Implement an API to handle GET requests

After learning about partial functions, you’ll use them as part of your implementation of an HTTP server. Building an HTTP server without the help of an external library would require lots of extra time and code. Thankfully, the Scala ecosystem offers a few external libraries to help you handle HTTP communication in an efficient and performant way. In this lesson, you’ll learn about http4s, a popular library to manage HTTP requests and responses. You’ll discover how to implement an HTTP server that replies to a GET /ping request with a response with status code 200 – Ok and the text "pong". Finally, you’ll see how to run it using sbt. In the capstone, you’ll use http4s to create an HTTP server and define its API.

17.1 An overview of http4s

Typelevel is a nonprofit organization to promote purely functional open source Scala projects together with an inclusive and welcoming environment (typelevel.org). http4s (http4s.org) is a Typelevel project and one of the most popular Scala libraries to handle HTTP requests and responses. It is particularly accessible to newcomers to the language, thanks to its extensive documentation and numerous examples.

17.2 A ping server using http4s

17.2.1 Initial setup

17.2.2 Implementing the API

17.2.3 Building a server

17.2.4 Let’s try it out!

Summary

Answers to quick checks

sitemap