\OCP\AppFramework\HttpJSONResponse

A renderer for JSON calls

It handles headers, HTTP status code, last modified and ETag.

Summary

Methods
Properties
Constants
cacheFor()
addCookie()
setCookies()
invalidateCookie()
invalidateCookies()
getCookies()
addHeader()
setHeaders()
getHeaders()
render()
setStatus()
setContentSecurityPolicy()
getContentSecurityPolicy()
getStatus()
getETag()
getLastModified()
setETag()
setLastModified()
__construct()
setData()
getData()
No public properties found
No constants found
No protected methods found
$data
N/A
No private methods found
$headers
$cookies
$status
$lastModified
$ETag
$contentSecurityPolicy
N/A

Properties

$data

$data : array|object

response data

Type

array|object

$headers

$headers : array

Headers - defaults to ['Cache-Control' => 'no-cache, must-revalidate']

Type

array

$cookies

$cookies : array

Cookies that will be need to be constructed as header

Type

array

$status

$status : integer

HTTP status code - defaults to STATUS OK

Type

integer

$lastModified

$lastModified : \DateTime

Last modified date

Type

\DateTime

$ETag

$ETag : string

ETag

Type

string

Methods

cacheFor()

cacheFor(integer  $cacheSeconds) : $this

Caches the response

Parameters

integer $cacheSeconds

the amount of seconds that should be cached if 0 then caching will be disabled

Returns

$this

addCookie()

addCookie(string  $name, string  $value, \DateTime|null  $expireDate = null) : $this

Adds a new cookie to the response

Parameters

string $name

The name of the cookie

string $value

The value of the cookie

\DateTime|null $expireDate

Date on that the cookie should expire, if set to null cookie will be considered as session cookie.

Returns

$this

setCookies()

setCookies(array  $cookies) : $this

Set the specified cookies

Parameters

array $cookies

array('foo' => array('value' => 'bar', 'expire' => null))

Returns

$this

invalidateCookie()

invalidateCookie(string  $name) : $this

Invalidates the specified cookie

Parameters

string $name

Returns

$this

invalidateCookies()

invalidateCookies(array  $cookieNames) : $this

Invalidates the specified cookies

Parameters

array $cookieNames

array('foo', 'bar')

Returns

$this

getCookies()

getCookies() : array

Returns the cookies

Returns

array

addHeader()

addHeader(string  $name, string  $value) : $this

Adds a new header to the response that will be called before the render function

Parameters

string $name

The name of the HTTP header

string $value

The value, null will delete it

Returns

$this

setHeaders()

setHeaders(array  $headers) : $this

Set the headers

Parameters

array $headers

value header pairs

Returns

$this

getHeaders()

getHeaders() : array

Returns the set headers

Returns

array —

the headers

render()

render() : string|null

Returns the rendered json

Throws

\Exception

If data could not get encoded

Returns

string|null —

the rendered json

setStatus()

setStatus(integer  $status) : \OCP\AppFramework\Http\Response

Set response status

Parameters

integer $status

a HTTP status code, see also the STATUS constants

Returns

\OCP\AppFramework\Http\Response

Reference to this object

setContentSecurityPolicy()

setContentSecurityPolicy(\OCP\AppFramework\Http\ContentSecurityPolicy  $csp) : $this

Set a Content-Security-Policy

Parameters

\OCP\AppFramework\Http\ContentSecurityPolicy $csp

Policy to set for the response object

Returns

$this

getContentSecurityPolicy()

getContentSecurityPolicy() : \OCP\AppFramework\Http\ContentSecurityPolicy|null

Get the currently used Content-Security-Policy

Returns

\OCP\AppFramework\Http\ContentSecurityPolicy|null —

Used Content-Security-Policy or null if none specified.

getStatus()

getStatus() 

Get response status

getETag()

getETag() : string

Get the ETag

Returns

string —

the etag

getLastModified()

getLastModified() : \DateTime

Get "last modified" date

Returns

\DateTime —

RFC2822 formatted last modified date

setETag()

setETag(string  $ETag) : \OCP\AppFramework\Http\Response

Set the ETag

Parameters

string $ETag

Returns

\OCP\AppFramework\Http\Response

Reference to this object

setLastModified()

setLastModified(\DateTime  $lastModified) : \OCP\AppFramework\Http\Response

Set "last modified" date

Parameters

\DateTime $lastModified

Returns

\OCP\AppFramework\Http\Response

Reference to this object

__construct()

__construct(array|object  $data = array(), integer  $statusCode = \OCP\AppFramework\Http::STATUS_OK) 

constructor of JSONResponse

Parameters

array|object $data

the object or array that should be transformed

integer $statusCode

the Http status code, defaults to 200

setData()

setData(array|object  $data) : \OCP\AppFramework\Http\JSONResponse

Sets values in the data json array

Parameters

array|object $data

an array or object which will be transformed to JSON

Returns

\OCP\AppFramework\Http\JSONResponse

Reference to this object

getData()

getData() : array

Used to get the set parameters

Returns

array —

the data