Interface Request

All Superinterfaces:
org.teavm.jso.JSObject

public interface Request extends org.teavm.jso.JSObject
An incoming HTTP request.

A body can be read once. Reading it twice raises, because it is a stream rather than a buffer.

Since:
1.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    default org.teavm.jso.typedarrays.ArrayBuffer
    Reads the whole body as bytes.
    default String
    Returns the Cloudflare data centre that handled the request, or null.
    default String
    Returns the two-letter country Cloudflare resolved the client to, or null.
    Returns Cloudflare's own request properties, such as country and colo.
    Returns the request headers.
    Returns the HTTP method.
    Returns the full request URL.
    default String
    Returns the host, including the port when the URL carried one.
    default String
    ip()
    Returns the client's IP address as Cloudflare saw it, or null.
    boolean
    Returns whether the body has already been read.
    default boolean
    Returns whether the request method is DELETE.
    default boolean
    Returns whether the request method is GET.
    default boolean
    Returns whether the request method is PATCH.
    default boolean
    Returns whether the request method is POST.
    default boolean
    Returns whether the request method is PUT.
    default TSObject
    Reads the whole body and parses it as JSON.
    default <T> T
    json(Class<T> type)
    Reads the whole body and converts it to a Java type.
    default <T> T
    json(Function<TSObject,T> mapper)
    Reads the whole body and converts it with a function of your own.
    default String
    Returns the scheme and host, with no path.
    default String
    Returns the request path, without the query string.
    default String
    query(String name)
    Reads one query parameter.
    default String
    query(String name, String fallback)
    Reads one query parameter, falling back when absent.
    default String
    Returns the ray id Cloudflare traces this request by, or null.
    org.teavm.jso.core.JSPromise<org.teavm.jso.typedarrays.ArrayBuffer>
     
    org.teavm.jso.core.JSPromise<TSObject>
     
    org.teavm.jso.core.JSPromise<org.teavm.jso.core.JSString>
     
    default String
    Reads the whole body as text.
    default String
    Returns the user agent string, or null.

    Methods inherited from interface org.teavm.jso.JSObject

    cast
  • Method Details

    • getUrl

      String getUrl()
      Returns the full request URL.
      Returns:
      the full request URL
    • getMethod

      String getMethod()
      Returns the HTTP method.
      Returns:
      the HTTP method
    • getHeaders

      Headers getHeaders()
      Returns the request headers.
      Returns:
      the request headers
    • isBodyUsed

      boolean isBodyUsed()
      Returns whether the body has already been read.
      Returns:
      whether the body has already been read
    • getCf

      TSObject getCf()
      Returns Cloudflare's own request properties, such as country and colo.
      Returns:
      Cloudflare's own request properties, such as country and colo
    • text

      default String text()
      Reads the whole body as text.
      Returns:
      the decoded body, empty when there is none
    • json

      default TSObject json()
      Reads the whole body and parses it as JSON.
      Returns:
      the parsed body
    • bytes

      default org.teavm.jso.typedarrays.ArrayBuffer bytes()
      Reads the whole body as bytes.
      Returns:
      the bytes
    • path

      default String path()
      Returns the request path, without the query string.
      Returns:
      the request path, without the query string
    • host

      default String host()
      Returns the host, including the port when the URL carried one.
      Returns:
      the host, including the port when the URL carried one
    • origin

      default String origin()
      Returns the scheme and host, with no path.
      Returns:
      the scheme and host, with no path
    • query

      default String query(String name)
      Reads one query parameter.
      Parameters:
      name - the parameter name
      Returns:
      the first value, or null when absent
    • query

      default String query(String name, String fallback)
      Reads one query parameter, falling back when absent.
      Parameters:
      name - the parameter name
      fallback - the value to use when absent
      Returns:
      the value, or the fallback
    • country

      default String country()
      Returns the two-letter country Cloudflare resolved the client to, or null.
      Returns:
      the two-letter country Cloudflare resolved the client to, or null
    • ip

      default String ip()
      Returns the client's IP address as Cloudflare saw it, or null.
      Returns:
      the client's IP address as Cloudflare saw it, or null
    • colo

      default String colo()
      Returns the Cloudflare data centre that handled the request, or null.

      Read from request.cf where it is present, and from the CF-Ray header otherwise. That header is <rayid>-<colo>, so only its suffix is the answer.

      Returns:
      the Cloudflare data centre that handled the request, or null
    • rayId

      default String rayId()
      Returns the ray id Cloudflare traces this request by, or null.
      Returns:
      the ray id Cloudflare traces this request by, or null
    • userAgent

      default String userAgent()
      Returns the user agent string, or null.
      Returns:
      the user agent string, or null
    • isGet

      default boolean isGet()
      Returns whether the request method is GET.
      Returns:
      whether the request method is GET
    • isPost

      default boolean isPost()
      Returns whether the request method is POST.
      Returns:
      whether the request method is POST
    • isPut

      default boolean isPut()
      Returns whether the request method is PUT.
      Returns:
      whether the request method is PUT
    • isPatch

      default boolean isPatch()
      Returns whether the request method is PATCH.
      Returns:
      whether the request method is PATCH
    • isDelete

      default boolean isDelete()
      Returns whether the request method is DELETE.
      Returns:
      whether the request method is DELETE
    • json

      default <T> T json(Class<T> type)
      Reads the whole body and converts it to a Java type.

      Needs a codec, which the Gradle plugin generates for any type annotated JSONType. A type without one raises rather than returning something half-converted; json(Function) is the way to read a type that has none.

       @JSONType
       record Order(String sku, int quantity) {}
      
       Order order = request.json(Order.class);
      
      Type Parameters:
      T - the type
      Parameters:
      type - the type to convert to
      Returns:
      the converted body
    • json

      default <T> T json(Function<TSObject,T> mapper)
      Reads the whole body and converts it with a function of your own.

      Needs no codec and no annotation, which makes it the direct route for a type the generator does not know about, and for a shape that is not a record at all.

       record Order(String sku, int quantity) {}
      
       Order order = request.json(body ->
       	new Order(body.get("sku").asString(), body.get("quantity").asInt())
       );
      
      Type Parameters:
      T - the type
      Parameters:
      mapper - reads the parsed body
      Returns:
      what the mapper returned
    • readText

      org.teavm.jso.core.JSPromise<org.teavm.jso.core.JSString> readText()
    • readJson

      org.teavm.jso.core.JSPromise<TSObject> readJson()
    • readBytes

      org.teavm.jso.core.JSPromise<org.teavm.jso.typedarrays.ArrayBuffer> readBytes()