Package com.google.cloud.functions
Interface HttpResponse
-
public interface HttpResponse
Represents the contents of an HTTP response that is being sent by a Cloud Function in response to an HTTP request.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
appendHeader(java.lang.String header, java.lang.String value)
Includes the given header name with the given value in the response.java.util.Optional<java.lang.String>
getContentType()
Returns theContent-Type
that was previously set bysetContentType(java.lang.String)
, or byappendHeader(java.lang.String, java.lang.String)
with a header name ofContent-Type
.java.util.Map<java.lang.String,java.util.List<java.lang.String>>
getHeaders()
Returns the headers that have been defined for the response so far.java.io.OutputStream
getOutputStream()
Returns anOutputStream
that can be used to write the body of the response.java.io.BufferedWriter
getWriter()
Returns aBufferedWriter
that can be used to write the text body of the response.void
setContentType(java.lang.String contentType)
Sets the value to use for theContent-Type
header in the response.void
setStatusCode(int code)
Sets the numeric HTTP status code to use in the response.void
setStatusCode(int code, java.lang.String message)
Sets the numeric HTTP status code and reason message to use in the response.
-
-
-
Method Detail
-
setStatusCode
void setStatusCode(int code)
Sets the numeric HTTP status code to use in the response. Most often this will be 200, which is the OK status. The named constants inHttpURLConnection
, such asHTTP_OK
, can be used as an alternative to writing numbers in your source code.- Parameters:
code
- the status code.
-
setStatusCode
void setStatusCode(int code, java.lang.String message)
Sets the numeric HTTP status code and reason message to use in the response. For example
setStatusCode(400, "Something went wrong")
. The named constants inHttpURLConnection
, such asHTTP_BAD_REQUEST
, can be used as an alternative to writing numbers in your source code.- Parameters:
code
- the status code.message
- the status message.
-
setContentType
void setContentType(java.lang.String contentType)
Sets the value to use for theContent-Type
header in the response. This may include a character encoding, for examplesetContentType("text/plain; charset=utf-8")
.- Parameters:
contentType
- the content type.
-
getContentType
java.util.Optional<java.lang.String> getContentType()
Returns theContent-Type
that was previously set bysetContentType(java.lang.String)
, or byappendHeader(java.lang.String, java.lang.String)
with a header name ofContent-Type
. If noContent-Type
has been set, returnsOptional.empty()
.- Returns:
- the content type, if any.
-
appendHeader
void appendHeader(java.lang.String header, java.lang.String value)
Includes the given header name with the given value in the response. This method may be called several times for the same header, in which case the response will contain the header the same number of times.- Parameters:
header
- an HTTP header, such asContent-Type
.value
- a value to associate with that header.
-
getHeaders
java.util.Map<java.lang.String,java.util.List<java.lang.String>> getHeaders()
Returns the headers that have been defined for the response so far. This will contain at least the headers that have been set viaappendHeader(java.lang.String, java.lang.String)
orsetContentType(java.lang.String)
, and may contain additional headers such asDate
.- Returns:
- a map where each key is a header name and the corresponding
List
value has one entry for every value associated with that header.
-
getOutputStream
java.io.OutputStream getOutputStream() throws java.io.IOException
Returns anOutputStream
that can be used to write the body of the response. This method is typically used to write binary data. If the body is text, thegetWriter()
method is more appropriate.- Returns:
- the output stream.
- Throws:
java.io.IOException
- if a validOutputStream
cannot be returned for some reason.java.lang.IllegalStateException
- ifgetWriter()
has already been called on this instance.
-
getWriter
java.io.BufferedWriter getWriter() throws java.io.IOException
Returns aBufferedWriter
that can be used to write the text body of the response. If the written text will not be US-ASCII, you should specify a character encoding by callingsetContentType("text/foo; charset=bar")
orappendHeader("Content-Type", "text/foo; charset=bar")
before calling this method.- Returns:
- the writer.
- Throws:
java.io.IOException
- if a validBufferedWriter
cannot be returned for some reason.java.lang.IllegalStateException
- ifgetOutputStream()
has already been called on this instance.
-
-