Why must service operations return HTTPResponse in EK9?

← Web Services · Ref: Q890

Every service method must return HTTPResponse. Returning Boolean, String, Integer, or any other type triggers E07750.

WHY HTTPRESPONSE

HTTP responses carry status codes, headers, content type, and body. Only HTTPResponse expresses all of these:

  status() — 200, 404, 500, etc.
  content() — response body
  contentType() — MIME type
  cacheControl() — caching directives
  contentLanguage() — response language

CORRECT PATTERN

  healthCheck() as GET for :/status
    <- response as HTTPResponse: ...

INCORRECT PATTERN

  healthCheck() as GET for :/status
    <- response <- true            // ERROR: E07750

See Q659 for HTTPResponse details. See Q657 for URI mapping. See Q660 for CRUD patterns.

Example

defines module qa.webdeep.servicereturntype

  defines service

    <?-
      API service with correct HTTPResponse return types.
    -?>
    StatusApi :/api open

      healthCheck() as GET for :/health
        <- response as HTTPResponse: () with trait of HTTPResponse
          override content()
            <- rtn as String: `{"status": "healthy"}`
          override status() as pure
            <- rtn as Integer: 200
          override contentType() as pure
            <- rtn as String: "application/json"
          override cacheControl() as pure
            <- rtn as String: "no-cache"
          override contentLanguage() as pure
            <- rtn as String: "en"
          default operator ?

  defines application

    StatusApp
      register StatusApi()

  defines program

    ServiceReturnTypeDemo()
      stdout <- Stdout()
      stdout.println("Service methods must return HTTPResponse")

Common mistakes

E07750 — Service methods must return HTTPResponse, not Boolean. The HTTP protocol requires status codes, headers, and content type, which only HTTPResponse can express. See ek9 -h E07750 for details.

Incorrect:

as Boolean

Correct:

as HTTPResponse
Other ways to ask this
  • What triggers E07750 SERVICE_INCOMPATIBLE_RETURN_TYPE?
  • Why can't my service method return String or Boolean?
  • What return type do EK9 service methods require?

Coming from another language?

Java: Spring returns any type or ResponseEntity. Python: Flask returns (body, status). Go: writes to http.ResponseWriter. Rust: HttpResponse value. EK9: mandates HTTPResponse trait, explicit status/content/type.

Keywords: service, web, incompatible, content, E07750, return, POST, GET, HTTPResponse, status