Model classes

sttp model is a stand-alone project which provides a basic HTTP model, along with constants for common HTTP header names, media types, and status codes.

The basic model classes are: Header, Cookie, CookieWithMeta, MediaType, Method, StatusCode and Uri. The .toString methods of these classes returns a representation as in a HTTP request/response. See the ScalaDoc for more information.

Constructors of the model classes are mostly private. Companion objects provide methods to construct model class instances, following these rules:

  • .parse(serialized: String): Either[String, ModelClass]: returns an error message or an instance of the model class
  • .unsafeApply(values): creates an instance of the model class; validates the input values and in case of an error, throws an exception. An error could be e.g. that the input values contain characters outside of the allowed range
  • .safeApply(...): Either[String, ModelClass]: same as above, but doesn’t throw exceptions. Instead, returns an error message or the model class instance
  • .notValidated(...): ModelClass: creates the model type, without validation, and without throwing exceptions

Moreover, companion objects provide constants and/or constructor methods for well-know model class instances. For example, there’s StatusCode.Ok, Method.POST, MediaType.ImageGif and Header.contentType(MediaType).

These constants are also available as traits: StatusCodes, MediaTypes and HeaderNames.

The model also contains aggregate/helper classes such as Headers and MultiQueryParams

Example with objects:

import sttp.client._

object Example {
  val request = basicRequest.header(Header.contentType(MediaType.ApplicationJson)).get(uri"")
  implicit val backend = HttpURLConnectionBackend()
  val response = request.send()
  if (response.code == StatusCode.Ok) println("Ok!")

Example with traits:

import sttp.client._

object Example extends HeaderNames with MediaTypes with StatusCodes {
  val request = basicRequest.header(ContentType, ApplicationJson.toString).get(uri"")

  implicit val backend = HttpURLConnectionBackend()
  val response = request.send()
  if (response.code == Ok) println("Ok!")
For more information see