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
Modifier and Type Method Description voidappendHeader(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-Typethat 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.OutputStreamgetOutputStream()Returns anOutputStreamthat can be used to write the body of the response.java.io.BufferedWritergetWriter()Returns aBufferedWriterthat can be used to write the text body of the response.voidsetContentType(java.lang.String contentType)Sets the value to use for theContent-Typeheader in the response.voidsetStatusCode(int code)Sets the numeric HTTP status code to use in the response.voidsetStatusCode(int code, java.lang.String message)Sets the numeric HTTP status code and reason message to use in the response.
-
Method Details
-
setStatusCode
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
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
Sets the value to use for theContent-Typeheader 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-Typethat was previously set bysetContentType(java.lang.String), or byappendHeader(java.lang.String, java.lang.String)with a header name ofContent-Type. If noContent-Typehas been set, returnsOptional.empty().- Returns:
- the content type, if any.
-
appendHeader
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
Listvalue has one entry for every value associated with that header.
-
getOutputStream
Returns anOutputStreamthat 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 validOutputStreamcannot be returned for some reason.java.lang.IllegalStateException- ifgetWriter()has already been called on this instance.
-
getWriter
Returns aBufferedWriterthat 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 validBufferedWritercannot be returned for some reason.java.lang.IllegalStateException- ifgetOutputStream()has already been called on this instance.
-