Package dev.gmitch215.bytebox
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 TypeMethodDescriptiondefault org.teavm.jso.typedarrays.ArrayBufferbytes()Reads the whole body as bytes.default Stringcolo()Returns the Cloudflare data centre that handled the request, ornull.default Stringcountry()Returns the two-letter country Cloudflare resolved the client to, ornull.getCf()Returns Cloudflare's own request properties, such ascountryandcolo.Returns the request headers.Returns the HTTP method.getUrl()Returns the full request URL.default Stringhost()Returns the host, including the port when the URL carried one.default Stringip()Returns the client's IP address as Cloudflare saw it, ornull.booleanReturns whether the body has already been read.default booleanisDelete()Returns whether the request method is DELETE.default booleanisGet()Returns whether the request method is GET.default booleanisPatch()Returns whether the request method is PATCH.default booleanisPost()Returns whether the request method is POST.default booleanisPut()Returns whether the request method is PUT.default TSObjectjson()Reads the whole body and parses it as JSON.default <T> TReads the whole body and converts it to a Java type.default <T> TReads the whole body and converts it with a function of your own.default Stringorigin()Returns the scheme and host, with no path.default Stringpath()Returns the request path, without the query string.default StringReads one query parameter.default StringReads one query parameter, falling back when absent.default StringrayId()Returns the ray id Cloudflare traces this request by, ornull.org.teavm.jso.core.JSPromise<org.teavm.jso.typedarrays.ArrayBuffer> org.teavm.jso.core.JSPromise<TSObject> readJson()org.teavm.jso.core.JSPromise<org.teavm.jso.core.JSString> readText()default Stringtext()Reads the whole body as text.default StringReturns the user agent string, ornull.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 ascountryandcolo.- Returns:
- Cloudflare's own request properties, such as
countryandcolo
-
text
Reads the whole body as text.- Returns:
- the decoded body, empty when there is none
-
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
Returns the request path, without the query string.- Returns:
- the request path, without the query 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
Returns the scheme and host, with no path.- Returns:
- the scheme and host, with no path
-
query
Reads one query parameter.- Parameters:
name- the parameter name- Returns:
- the first value, or
nullwhen absent
-
query
Reads one query parameter, falling back when absent.- Parameters:
name- the parameter namefallback- the value to use when absent- Returns:
- the value, or the fallback
-
country
Returns the two-letter country Cloudflare resolved the client to, ornull.- Returns:
- the two-letter country Cloudflare resolved the client to, or
null
-
ip
Returns the client's IP address as Cloudflare saw it, ornull.- Returns:
- the client's IP address as Cloudflare saw it, or
null
-
colo
Returns the Cloudflare data centre that handled the request, ornull.Read from
request.cfwhere it is present, and from theCF-Rayheader 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
Returns the ray id Cloudflare traces this request by, ornull.- Returns:
- the ray id Cloudflare traces this request by, or
null
-
userAgent
Returns the user agent string, ornull.- 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
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
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()
-