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
    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 the Content-Type that was previously set by setContentType(java.lang.String), or by appendHeader(java.lang.String, java.lang.String) with a header name of Content-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 an OutputStream that can be used to write the body of the response.
    java.io.BufferedWriter getWriter()
    Returns a BufferedWriter that can be used to write the text body of the response.
    void setContentType​(java.lang.String contentType)
    Sets the value to use for the Content-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 Details

    • 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 in HttpURLConnection, such as HTTP_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 in HttpURLConnection, such as HTTP_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 the Content-Type header in the response. This may include a character encoding, for example setContentType("text/plain; charset=utf-8").
      Parameters:
      contentType - the content type.
    • getContentType

      java.util.Optional<java.lang.String> getContentType()
      Returns the Content-Type that was previously set by setContentType(java.lang.String), or by appendHeader(java.lang.String, java.lang.String) with a header name of Content-Type. If no Content-Type has been set, returns Optional.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 as Content-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 via appendHeader(java.lang.String, java.lang.String) or setContentType(java.lang.String), and may contain additional headers such as Date.
      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 an OutputStream 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, the getWriter() method is more appropriate.
      Returns:
      the output stream.
      Throws:
      java.io.IOException - if a valid OutputStream cannot be returned for some reason.
      java.lang.IllegalStateException - if getWriter() has already been called on this instance.
    • getWriter

      java.io.BufferedWriter getWriter() throws java.io.IOException
      Returns a BufferedWriter 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 calling setContentType("text/foo; charset=bar") or appendHeader("Content-Type", "text/foo; charset=bar") before calling this method.
      Returns:
      the writer.
      Throws:
      java.io.IOException - if a valid BufferedWriter cannot be returned for some reason.
      java.lang.IllegalStateException - if getOutputStream() has already been called on this instance.