Package io.nitric.http
Class HttpResponse
- java.lang.Object
-
- io.nitric.http.HttpResponse
-
public class HttpResponse extends Object
Provides an immutable HTTP response class. This class provides static convenience methods for quickly creating a response and a
HttpResponse.Builder
class for fine grained control.The HTTP examples below use the static
HttpResponse
build methods:import io.nitric.http.HttpResponse; ... // 404 - Not Found response return HttpResponse.build(404); // 200 - JSON message var json = "{ \"status\": \"online\" }"; return HttpResponse.build(200, json); // 418 - Error message return HttpResponse.build(418, "Im a tea pot");
The example below uses the
HttpResponse.Builder
class:import io.nitric.http.HttpResponse; ... byte[] data = Files.readAllBytes(path); return HttpResponse.newBuilder() .header("Content-Type", "application/jar") .body(data) .build();
- See Also:
HttpRequest
,HttpHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpResponse.Builder
Provides a HTTP response builder class.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static HttpResponse
build(int status)
Return a new HTTP response object from the given status.static HttpResponse
build(int status, String body)
Return a new HttpResponse object from the given Nitric status and body text.static HttpResponse
build(String body)
Return a new HTTP response object from the given body text.byte[]
getBody()
int
getBodyLength()
String
getBodyText()
String
getHeader(String name)
Return the named response header or null if not found.Map<String,List<String>>
getHeaders()
int
getStatus()
static HttpResponse.Builder
newBuilder()
String
toString()
-
-
-
Method Detail
-
getStatus
public int getStatus()
- Returns:
- the HTTP response status, e.g. 200 for HTTP OK
-
getHeaders
public Map<String,List<String>> getHeaders()
- Returns:
- an immutable map of function response headers
-
getHeader
public String getHeader(String name)
Return the named response header or null if not found. If the header has multiple values the first value will be returned.- Parameters:
name
- the name of the Nitric header- Returns:
- the named function header or null if not found
-
getBody
public byte[] getBody()
- Returns:
- the response body
-
getBodyText
public String getBodyText()
- Returns:
- the response body as text (UTF-8 encoded)
-
getBodyLength
public int getBodyLength()
- Returns:
- the function response body length, or -1 if no body present.
-
toString
public String toString()
-
newBuilder
public static HttpResponse.Builder newBuilder()
- Returns:
- a new HTTP response builder class.
-
build
public static HttpResponse build(String body)
Return a new HTTP response object from the given body text.- Parameters:
body
- the HTTP response body- Returns:
- a new HttpResponse object
-
build
public static HttpResponse build(int status)
Return a new HTTP response object from the given status.- Parameters:
status
- the HTTP response status- Returns:
- a new HttpResponse object
-
build
public static HttpResponse build(int status, String body)
Return a new HttpResponse object from the given Nitric status and body text.- Parameters:
status
- the response statusbody
- the body text- Returns:
- a new HttpResponse object
-
-