blob: 7eed6ef3635449bc835e12b36ba2ce0b96b5bbde [file] [log] [blame]
Shawn O. Pearceec9efd72012-04-04 20:44:39 -07001Gerrit Code Review - REST API
2=============================
3
4Gerrit Code Review comes with a REST like API available over HTTP.
5The API is suitable for automated tools to build upon, as well as
6supporting some ad-hoc scripting use cases.
7
David Pursehouseed321322013-05-17 13:53:32 +01008See also: link:dev-rest-api.html[REST API Developers' Notes].
David Pursehouse567e57b2013-05-07 16:41:48 +01009
Edwin Kempin361ed762013-04-03 13:52:24 +020010Endpoints
11---------
Edwin Kempin87340e62013-06-24 16:46:34 +020012link:rest-api-access.html[/access/]::
13 Access Right related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020014link:rest-api-accounts.html[/accounts/]::
15 Account related REST endpoints
16link:rest-api-changes.html[/changes/]::
17 Change related REST endpoints
David Ostrovsky28b8ea62013-06-09 02:16:57 +020018link:rest-api-config.html[/config/]::
19 Config related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020020link:rest-api-groups.html[/groups/]::
21 Group related REST endpoints
Edwin Kempin36eeee22013-08-30 15:57:34 +020022link:rest-api-plugins.html[/plugins/]::
23 Plugin related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020024link:rest-api-projects.html[/projects/]::
25 Project related REST endpoints
26
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070027Protocol Details
28----------------
29
Edwin Kempind089fc42012-07-18 14:58:40 +020030[[authentication]]
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070031Authentication
32~~~~~~~~~~~~~~
33By default all REST endpoints assume anonymous access and filter
34results to correspond to what anonymous users can read (which may
35be nothing at all).
36
37Users (and programs) may authenticate using HTTP authentication by
38supplying the HTTP password from the user's account settings page.
39Gerrit by default uses HTTP digest authentication. To authenticate,
40prefix the endpoint URL with `/a/`. For example to authenticate to
41`/projects/` request URL `/a/projects/`.
42
Shawn Pearcea90a43a2013-01-26 12:50:56 -080043[[preconditions]]
44Preconditions
45~~~~~~~~~~~~~
46Clients can request PUT to create a new resource and not overwrite
47an existing one by adding `If-None-Match: *` to the request HTTP
48headers. If the named resource already exists the server will respond
49with HTTP 412 Precondition Failed.
50
Edwin Kempind089fc42012-07-18 14:58:40 +020051[[output]]
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070052Output Format
53~~~~~~~~~~~~~
Shawn O. Pearceea6d0b52012-11-16 10:57:31 -080054Most APIs return pretty printed JSON by default. Compact JSON can be
55requested by setting the `Accept` HTTP request header to include
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070056`application/json`, for example:
57
58----
59 GET /projects/ HTTP/1.0
60 Accept: application/json
61----
62
63JSON responses are encoded using UTF-8 and use content type
David Pursehousef6c0bec2013-08-26 16:31:51 +090064`application/json`.
65
66To prevent against Cross Site Script Inclusion (XSSI) attacks, the JSON
67response body starts with a magic prefix line that must be stripped before
68feeding the rest of the response body to a JSON parser:
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070069
70----
71 )]}'
72 [ ... valid JSON ... ]
73----
74
Shawn O. Pearceea6d0b52012-11-16 10:57:31 -080075The default JSON format is pretty, which uses extra whitespace to make
76the output more readable for a human. Producing (and parsing) the
77non-pretty compact format is more efficient so tools should request it
78by using the `Accept: application/json` header or `pp=0` query
79parameter whenever possible.
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070080
81Responses will be gzip compressed by the server if the HTTP
82`Accept-Encoding` request header is set to `gzip`. This may
83save on network transfer time for larger responses.
84
Edwin Kempine3446292013-02-19 16:40:14 +010085[[timestamp]]
86Timestamp
87~~~~~~~~~
88Timestamps are given in UTC and have the format
89"'yyyy-mm-dd hh:mm:ss.fffffffff'" where "'ffffffffff'" indicates the
90nanoseconds.
91
Edwin Kempin7fe2f7f2013-02-06 10:12:52 +010092[[encoding]]
93Encoding
94~~~~~~~~
95All IDs that appear in the URL of a REST call (e.g. project name, group name)
96must be URL encoded.
97
Edwin Kempine026a7d2013-03-13 11:03:42 +010098[[response-codes]]
99Response Codes
100~~~~~~~~~~~~~~
101HTTP status codes are well defined and the Gerrit REST endpoints use
102them as described in the HTTP spec.
103
104Here are examples for some HTTP status codes that show how they are
105used in the context of the Gerrit REST API.
106
107400 Bad Request
108^^^^^^^^^^^^^^^
109`400 Bad Request` is used if the request is not understood by the
110server due to malformed syntax.
111
112E.g. `400 Bad Request` is returned if JSON input is expected but the
113'Content-Type' of the request is not 'application/json' or the request
114body doesn't contain valid JSON.
115
116`400 Bad Request` is also used if required input fields are not set or
117if options are set which cannot be used together.
118
119403 Forbidden
120^^^^^^^^^^^^^
121`403 Forbidden` is used if the operation is not allowed because the
122calling user has no sufficient permissions.
123
124E.g. some REST endpoints require that the calling user has certain
David Pursehouse8becc2a2013-04-23 18:51:04 +0900125link:access-control.html#global_capabilities[global capabilities]
Edwin Kempine026a7d2013-03-13 11:03:42 +0100126assigned.
127
128`403 Forbidden` is also used if `self` is used as account ID and the
129REST call was done without authentication.
130
131404 Not Found
132^^^^^^^^^^^^^
133`404 Not Found` is returned if the resource that is specified by the
134URL is not found or is not visible to the calling user. A resource
135cannot be found if the URL contains a non-existing ID or view.
136
137405 Method Not Allowed
138^^^^^^^^^^^^^^^^^^^^^^
139`405 Method Not Allowed` is used if the resource exists but doesn't
140support the operation.
141
142E.g. some of the `/groups/` endpoints are only supported for Gerrit
143internal groups, if they are invoked for an external group the response
144is `405 Method Not Allowed`.
145
146409 Conflict
147^^^^^^^^^^^^
148`409 Conflict` is used if the request cannot be completed because the
149current state of the resource doesn't allow the operation.
150
151E.g. if you try to submit a change that is abandoned, this fails with
152`409 Conflict` because the state of the change doesn't allow the submit
153operation.
154
155`409 Conflict` is also used if you try to create a resource but the
156name is already occupied by an existing resource.
157
158412 Precondition Failed
159^^^^^^^^^^^^^^^^^^^^^^^
160`412 Precondition Failed` is used if a precondition from the request
161header fields is not fulfilled as described in the link:#preconditions[
162Preconditions] section.
163
164422 Unprocessable Entity
165^^^^^^^^^^^^^^^^^^^^^^^^
166`422 Unprocessable Entity` is returned if the ID of a resource that is
167specified in the request body cannot be resolved.
168
Shawn O. Pearceec9efd72012-04-04 20:44:39 -0700169GERRIT
170------
171Part of link:index.html[Gerrit Code Review]