blob: 348af767716913089a36dafb8bd4cff499e143aa [file] [log] [blame]
Marian Harbachebeb1542019-12-13 10:42:46 +01001:linkattrs:
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002= Gerrit Code Review - REST API
Shawn O. Pearceec9efd72012-04-04 20:44:39 -07003
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
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080010== Endpoints
Edwin Kempin87340e62013-06-24 16:46:34 +020011link:rest-api-access.html[/access/]::
12 Access Right related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020013link:rest-api-accounts.html[/accounts/]::
14 Account related REST endpoints
15link:rest-api-changes.html[/changes/]::
16 Change related REST endpoints
David Ostrovsky28b8ea62013-06-09 02:16:57 +020017link:rest-api-config.html[/config/]::
18 Config related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020019link:rest-api-groups.html[/groups/]::
20 Group related REST endpoints
Edwin Kempin36eeee22013-08-30 15:57:34 +020021link:rest-api-plugins.html[/plugins/]::
22 Plugin related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020023link:rest-api-projects.html[/projects/]::
24 Project related REST endpoints
Yuxuan 'fishy' Wangec4b06a2013-10-14 12:39:30 -070025link:rest-api-documentation.html[/Documentation/]::
26 Documentation related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020027
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080028== Protocol Details
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070029
Edwin Kempind089fc42012-07-18 14:58:40 +020030[[authentication]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080031=== Authentication
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070032By default all REST endpoints assume anonymous access and filter
33results to correspond to what anonymous users can read (which may
34be nothing at all).
35
Shawn Pearced79dcef2017-06-10 11:57:30 -070036Users (and programs) can authenticate with HTTP passwords by prefixing
37the endpoint URL with `/a/`. For example to authenticate to
38`/projects/`, request the URL `/a/projects/`. Gerrit will use HTTP basic
39authentication with the HTTP password from the user's account settings
40page. This form of authentication bypasses the need for XSRF tokens.
David Pursehouse62fe0402014-05-01 17:14:35 +090041
Shawn Pearced79dcef2017-06-10 11:57:30 -070042An authorization cookie may be presented in the request URL inside the
43`access_token` query parameter. XSRF tokens are not required when a
44valid `access_token` is used in the URL.
45
46[[cors]]
47=== CORS
48
49Cross-site scripting may be supported if the administrator has configured
50link:config-gerrit.html#site.allowOriginRegex[site.allowOriginRegex].
51
52Approved web applications running from an allowed origin can rely on
53CORS preflight to authorize requests requiring cookie based
54authentication, or mutations (POST, PUT, DELETE). Mutations require a
55valid XSRF token in the `X-Gerrit-Auth` request header.
56
57Alternatively applications can use `access_token` in the URL (see
58above) to authorize requests. Mutations sent as POST with a request
59content type of `text/plain` can skip CORS preflight. Gerrit accepts
60additional query parameters `$m` to override the correct method (PUT,
61POST, DELETE) and `$ct` to specify the actual content type, such as
62`application/json; charset=UTF-8`. Example:
63
64----
65 POST /changes/42/topic?$m=PUT&$ct=application/json%3B%20charset%3DUTF-8&access_token=secret HTTP/1.1
66 Content-Type: text/plain
67 Content-Length: 23
68
69 {"topic": "new-topic"}
70----
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070071
Shawn Pearcea90a43a2013-01-26 12:50:56 -080072[[preconditions]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080073=== Preconditions
Shawn Pearcea90a43a2013-01-26 12:50:56 -080074Clients can request PUT to create a new resource and not overwrite
75an existing one by adding `If-None-Match: *` to the request HTTP
76headers. If the named resource already exists the server will respond
77with HTTP 412 Precondition Failed.
78
Edwin Kempine2f91032022-10-12 13:43:03 +020079[[backwards-compatibility]]
80=== Backwards Compatibility
81
82The REST API is regularly extended (e.g. addition of new REST endpoints or new fields in existing
83JSON entities). Callers of the REST API must be able to deal with this (e.g. ignore unknown fields
84in the REST responses). Incompatible changes (e.g. removal of REST endpoints, altering/removal of
85existing fields in JSON entities) are avoided if possible, but can happen in rare cases. If they
86happen, they are announced in the link:https://www.gerritcodereview.com/releases-readme.html[release
87notes].
88
Edwin Kempind089fc42012-07-18 14:58:40 +020089[[output]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080090=== Output Format
David Pursehouse0ddcb082014-05-01 21:50:18 +090091JSON responses are encoded using UTF-8 and use content type
92`application/json`.
93
94By default most APIs return pretty-printed JSON, which uses extra
95whitespace to make the output more readable for humans.
96
97Compact JSON can be requested by setting the `pp=0` query parameter,
98or by setting the `Accept` HTTP request header to include `application/json`:
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070099
100----
101 GET /projects/ HTTP/1.0
102 Accept: application/json
103----
104
David Pursehouse0ddcb082014-05-01 21:50:18 +0900105Producing (and parsing) the non-pretty compact format is more efficient,
106so tools should request it whenever possible.
David Pursehousef6c0bec2013-08-26 16:31:51 +0900107
108To prevent against Cross Site Script Inclusion (XSSI) attacks, the JSON
109response body starts with a magic prefix line that must be stripped before
110feeding the rest of the response body to a JSON parser:
Shawn O. Pearceec9efd72012-04-04 20:44:39 -0700111
112----
113 )]}'
114 [ ... valid JSON ... ]
115----
116
Shawn O. Pearceec9efd72012-04-04 20:44:39 -0700117Responses will be gzip compressed by the server if the HTTP
118`Accept-Encoding` request header is set to `gzip`. This may
119save on network transfer time for larger responses.
120
Patrick Hieseld527c352017-04-28 12:44:10 +0200121[[input]]
122=== Input Format
123Unknown JSON parameters will simply be ignored by Gerrit without causing
124an exception. This also applies to case-sensitive parameters, such as
125map keys.
126
Edwin Kempine3446292013-02-19 16:40:14 +0100127[[timestamp]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800128=== Timestamp
Edwin Kempine3446292013-02-19 16:40:14 +0100129Timestamps are given in UTC and have the format
David Pursehouse0ddcb082014-05-01 21:50:18 +0900130"'yyyy-mm-dd hh:mm:ss.fffffffff'" where "'ffffffffff'" represents
Edwin Kempine3446292013-02-19 16:40:14 +0100131nanoseconds.
132
Edwin Kempin7fe2f7f2013-02-06 10:12:52 +0100133[[encoding]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800134=== Encoding
Edwin Kempin7fe2f7f2013-02-06 10:12:52 +0100135All IDs that appear in the URL of a REST call (e.g. project name, group name)
136must be URL encoded.
137
Edwin Kempine026a7d2013-03-13 11:03:42 +0100138[[response-codes]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800139=== Response Codes
David Pursehouse62fe0402014-05-01 17:14:35 +0900140The Gerrit REST endpoints use HTTP status codes as described
141in the link:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html[
Marian Harbach34253372019-12-10 18:01:31 +0100142HTTP specification,role=external,window=_blank].
Edwin Kempine026a7d2013-03-13 11:03:42 +0100143
Dave Borowitz2f692b22016-07-28 10:04:03 -0400144In most cases, the response body of an error response will be a
145plaintext, human-readable error message.
146
David Pursehouse62fe0402014-05-01 17:14:35 +0900147Here are examples that show how HTTP status codes are used in the
148context of the Gerrit REST API.
Edwin Kempine026a7d2013-03-13 11:03:42 +0100149
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800150==== 400 Bad Request
David Pursehouse10829f642015-01-21 11:42:32 +0900151"`400 Bad Request`" is returned if the request is not understood by the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100152server due to malformed syntax.
153
David Pursehouse10829f642015-01-21 11:42:32 +0900154E.g. "`400 Bad Request`" is returned if JSON input is expected but the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100155'Content-Type' of the request is not 'application/json' or the request
156body doesn't contain valid JSON.
157
David Pursehouse10829f642015-01-21 11:42:32 +0900158"`400 Bad Request`" is also returned if required input fields are not set or
Edwin Kempine026a7d2013-03-13 11:03:42 +0100159if options are set which cannot be used together.
160
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800161==== 403 Forbidden
David Pursehouse10829f642015-01-21 11:42:32 +0900162"`403 Forbidden`" is returned if the operation is not allowed because the
David Pursehouse62fe0402014-05-01 17:14:35 +0900163calling user does not have sufficient permissions.
Edwin Kempine026a7d2013-03-13 11:03:42 +0100164
165E.g. some REST endpoints require that the calling user has certain
David Pursehouse8becc2a2013-04-23 18:51:04 +0900166link:access-control.html#global_capabilities[global capabilities]
Edwin Kempine026a7d2013-03-13 11:03:42 +0100167assigned.
168
David Pursehouse10829f642015-01-21 11:42:32 +0900169"`403 Forbidden`" is also returned if `self` is used as account ID and the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100170REST call was done without authentication.
171
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800172==== 404 Not Found
David Pursehouse10829f642015-01-21 11:42:32 +0900173"`404 Not Found`" is returned if the resource that is specified by the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100174URL is not found or is not visible to the calling user. A resource
175cannot be found if the URL contains a non-existing ID or view.
176
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800177==== 405 Method Not Allowed
David Pursehouse10829f642015-01-21 11:42:32 +0900178"`405 Method Not Allowed`" is returned if the resource exists but doesn't
Edwin Kempine026a7d2013-03-13 11:03:42 +0100179support the operation.
180
181E.g. some of the `/groups/` endpoints are only supported for Gerrit
David Pursehouse0ddcb082014-05-01 21:50:18 +0900182internal groups; if they are invoked for an external group the response
David Pursehouse10829f642015-01-21 11:42:32 +0900183is "`405 Method Not Allowed`".
Edwin Kempine026a7d2013-03-13 11:03:42 +0100184
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800185==== 409 Conflict
David Pursehouse10829f642015-01-21 11:42:32 +0900186"`409 Conflict`" is returned if the request cannot be completed because the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100187current state of the resource doesn't allow the operation.
188
189E.g. if you try to submit a change that is abandoned, this fails with
David Pursehouse10829f642015-01-21 11:42:32 +0900190"`409 Conflict`" because the state of the change doesn't allow the submit
Edwin Kempine026a7d2013-03-13 11:03:42 +0100191operation.
192
David Pursehouse10829f642015-01-21 11:42:32 +0900193"`409 Conflict`" is also returned if you try to create a resource but the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100194name is already occupied by an existing resource.
195
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800196==== 412 Precondition Failed
David Pursehouse10829f642015-01-21 11:42:32 +0900197"`412 Precondition Failed`" is returned if a precondition from the request
David Pursehouse62fe0402014-05-01 17:14:35 +0900198header fields is not fulfilled, as described in the link:#preconditions[
Edwin Kempine026a7d2013-03-13 11:03:42 +0100199Preconditions] section.
200
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800201==== 422 Unprocessable Entity
David Pursehouse10829f642015-01-21 11:42:32 +0900202"`422 Unprocessable Entity`" is returned if the ID of a resource that is
Edwin Kempine026a7d2013-03-13 11:03:42 +0100203specified in the request body cannot be resolved.
204
Patrick Hieselc0a28ce2018-11-15 15:28:06 +0100205==== 429 Too Many Requests
206"`429 Too Many Requests`" is returned if the request exhausted any set
207quota limits. Depending on the exhausted quota, the request may be retried
208with exponential backoff.
209
Edwin Kempin0ade5aa2018-08-10 08:49:32 +0200210[[tracing]]
211=== Request Tracing
Edwin Kempinda823782018-08-29 22:52:54 +0200212For each REST endpoint tracing can be enabled by setting the
213`trace=<trace-id>` request parameter. It is recommended to use the ID
214of the issue that is being investigated as trace ID.
215
216.Example Request
217----
218 GET /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/suggest_reviewers?trace=issue/123&q=J
219----
220
221It is also possible to omit the trace ID and get a unique trace ID
222generated.
Edwin Kempin0ade5aa2018-08-10 08:49:32 +0200223
224.Example Request
225----
226 GET /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/suggest_reviewers?trace&q=J
227----
228
Edwin Kempin99ccd922018-09-10 10:06:53 +0200229Alternatively request tracing can also be enabled by setting the
230`X-Gerrit-Trace` header:
231
232.Example Request
233----
234 GET /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/suggest_reviewers?q=J
235 X-Gerrit-Trace: issue/123
236----
237
Edwin Kempin0ade5aa2018-08-10 08:49:32 +0200238Enabling tracing results in additional logs with debug information that
239are written to the `error_log`. All logs that correspond to the traced
Edwin Kempinda823782018-08-29 22:52:54 +0200240request are associated with the trace ID. The trace ID is returned with
241the REST response in the `X-Gerrit-Trace` header.
Edwin Kempin0ade5aa2018-08-10 08:49:32 +0200242
243.Example Response
244----
245HTTP/1.1 200 OK
246 Content-Disposition: attachment
247 Content-Type: application/json; charset=UTF-8
248 X-Gerrit-Trace: 1533885943749-8257c498
249
250 )]}'
251 ... <json> ...
252----
253
254Given the trace ID an administrator can find the corresponding logs and
255investigate issues more easily.
256
Edwin Kempin0a5c1612021-07-30 09:35:30 +0200257[[deadline]]
258=== Setting a deadline
259
260When invoking a REST endpoint it's possible that the client sets a deadline
261after which the request should be aborted. To do this the `X-Gerrit-Deadline`
262header must be set on the request. Values must be specified using standard time
263unit abbreviations ('ms', 'sec', 'min', etc.).
264
265.Example Request
266----
267 GET /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/suggest_reviewers?q=J
268 X-Gerrit-Deadline: 5m
269----
270
Edwin Kempin927ff822021-08-03 15:46:34 +0200271
272Setting a deadline on the request overrides any
273link:config-gerrit.html#deadline.id[server-side deadline] that has been
274configured on the host.
275
Gal Paikin756f5612021-06-08 13:38:00 +0200276[[updated-refs]]
277=== X-Gerrit-UpdatedRef
Gal Paikin676dc512021-06-28 16:13:08 +0200278This is only enabled when "X-Gerrit-UpdatedRef-Enabled" is set to "true" in the
279request header.
280
Gal Paikin756f5612021-06-08 13:38:00 +0200281For each write REST request, we return X-Gerrit-UpdatedRef headers as the refs
282that were updated in the current request (involved in a ref transaction in the
283current request).
284
Gal Paikineb0854b2021-06-28 16:26:54 +0200285The format of those headers is `PROJECT_NAME\~REF_NAME\~OLD_SHA-1\~NEW_SHA-1`.
286The project and ref names are URL-encoded, and must use %7E for '~'.
Gal Paikin756f5612021-06-08 13:38:00 +0200287
288A new SHA-1 of `0000000000000000000000000000000000000000` is treated as a
289deleted ref.
290If the new SHA-1 is not `0000000000000000000000000000000000000000`, the ref was
291either updated or created.
292If the old SHA-1 is `0000000000000000000000000000000000000000`, the ref was
293created.
294
295.Example Request
296----
297 DELETE /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940
298----
299
300.Example Response
301----
302HTTP/1.1 204 NO CONTENT
303 Content-Disposition: attachment
304 Content-Type: application/json; charset=UTF-8
305 X-Gerrit-UpdatedRef: myProject~refs%2Fchanges%2F01%2F1%2F1~deadbeefdeadbeefdeadbeefdeadbeefdeadbeef~0000000000000000000000000000000000000000
306 X-Gerrit-UpdatedRef: myProject~refs%2Fchanges%2F01%2F1%2Fmeta~deadbeefdeadbeefdeadbeefdeadbeefdeadbeef~0000000000000000000000000000000000000000
307
308 )]}'
309----
310
Shawn O. Pearceec9efd72012-04-04 20:44:39 -0700311GERRIT
312------
313Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -0700314
315SEARCHBOX
316---------