blob: a87f5b6c776ca0d6c156ab19fff06413456dd2db [file] [log] [blame]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001= Gerrit Code Review - REST API
Shawn O. Pearceec9efd72012-04-04 20:44:39 -07002
3Gerrit Code Review comes with a REST like API available over HTTP.
4The API is suitable for automated tools to build upon, as well as
5supporting some ad-hoc scripting use cases.
6
David Pursehouseed321322013-05-17 13:53:32 +01007See also: link:dev-rest-api.html[REST API Developers' Notes].
David Pursehouse567e57b2013-05-07 16:41:48 +01008
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08009== Endpoints
Edwin Kempin87340e62013-06-24 16:46:34 +020010link:rest-api-access.html[/access/]::
11 Access Right related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020012link:rest-api-accounts.html[/accounts/]::
13 Account related REST endpoints
14link:rest-api-changes.html[/changes/]::
15 Change related REST endpoints
David Ostrovsky28b8ea62013-06-09 02:16:57 +020016link:rest-api-config.html[/config/]::
17 Config related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020018link:rest-api-groups.html[/groups/]::
19 Group related REST endpoints
Edwin Kempin36eeee22013-08-30 15:57:34 +020020link:rest-api-plugins.html[/plugins/]::
21 Plugin related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020022link:rest-api-projects.html[/projects/]::
23 Project related REST endpoints
Yuxuan 'fishy' Wangec4b06a2013-10-14 12:39:30 -070024link:rest-api-documentation.html[/Documentation/]::
25 Documentation related REST endpoints
Edwin Kempin361ed762013-04-03 13:52:24 +020026
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080027== Protocol Details
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070028
Edwin Kempind089fc42012-07-18 14:58:40 +020029[[authentication]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080030=== Authentication
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070031By default all REST endpoints assume anonymous access and filter
32results to correspond to what anonymous users can read (which may
33be nothing at all).
34
David Pursehouse62fe0402014-05-01 17:14:35 +090035Users (and programs) may authenticate by prefixing the endpoint URL with
36`/a/`. For example to authenticate to `/projects/`, request the URL
37`/a/projects/`.
38
39By default Gerrit uses HTTP digest authentication with the HTTP password
40from the user's account settings page. HTTP basic authentication is used
41if link:config-gerrit.html#auth.gitBasicAuth[`auth.gitBasicAuth`] is set
42to true in the Gerrit configuration.
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070043
Shawn Pearcea90a43a2013-01-26 12:50:56 -080044[[preconditions]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080045=== Preconditions
Shawn Pearcea90a43a2013-01-26 12:50:56 -080046Clients 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]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080052=== Output Format
David Pursehouse0ddcb082014-05-01 21:50:18 +090053JSON responses are encoded using UTF-8 and use content type
54`application/json`.
55
56By default most APIs return pretty-printed JSON, which uses extra
57whitespace to make the output more readable for humans.
58
59Compact JSON can be requested by setting the `pp=0` query parameter,
60or by setting the `Accept` HTTP request header to include `application/json`:
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070061
62----
63 GET /projects/ HTTP/1.0
64 Accept: application/json
65----
66
David Pursehouse0ddcb082014-05-01 21:50:18 +090067Producing (and parsing) the non-pretty compact format is more efficient,
68so tools should request it whenever possible.
David Pursehousef6c0bec2013-08-26 16:31:51 +090069
70To prevent against Cross Site Script Inclusion (XSSI) attacks, the JSON
71response body starts with a magic prefix line that must be stripped before
72feeding the rest of the response body to a JSON parser:
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070073
74----
75 )]}'
76 [ ... valid JSON ... ]
77----
78
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070079Responses will be gzip compressed by the server if the HTTP
80`Accept-Encoding` request header is set to `gzip`. This may
81save on network transfer time for larger responses.
82
Edwin Kempine3446292013-02-19 16:40:14 +010083[[timestamp]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080084=== Timestamp
Edwin Kempine3446292013-02-19 16:40:14 +010085Timestamps are given in UTC and have the format
David Pursehouse0ddcb082014-05-01 21:50:18 +090086"'yyyy-mm-dd hh:mm:ss.fffffffff'" where "'ffffffffff'" represents
Edwin Kempine3446292013-02-19 16:40:14 +010087nanoseconds.
88
Edwin Kempin7fe2f7f2013-02-06 10:12:52 +010089[[encoding]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080090=== Encoding
Edwin Kempin7fe2f7f2013-02-06 10:12:52 +010091All IDs that appear in the URL of a REST call (e.g. project name, group name)
92must be URL encoded.
93
Edwin Kempine026a7d2013-03-13 11:03:42 +010094[[response-codes]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080095=== Response Codes
David Pursehouse62fe0402014-05-01 17:14:35 +090096The Gerrit REST endpoints use HTTP status codes as described
97in the link:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html[
98HTTP specification].
Edwin Kempine026a7d2013-03-13 11:03:42 +010099
David Pursehouse62fe0402014-05-01 17:14:35 +0900100Here are examples that show how HTTP status codes are used in the
101context of the Gerrit REST API.
Edwin Kempine026a7d2013-03-13 11:03:42 +0100102
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800103==== 400 Bad Request
David Pursehouse10829f642015-01-21 11:42:32 +0900104"`400 Bad Request`" is returned if the request is not understood by the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100105server due to malformed syntax.
106
David Pursehouse10829f642015-01-21 11:42:32 +0900107E.g. "`400 Bad Request`" is returned if JSON input is expected but the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100108'Content-Type' of the request is not 'application/json' or the request
109body doesn't contain valid JSON.
110
David Pursehouse10829f642015-01-21 11:42:32 +0900111"`400 Bad Request`" is also returned if required input fields are not set or
Edwin Kempine026a7d2013-03-13 11:03:42 +0100112if options are set which cannot be used together.
113
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800114==== 403 Forbidden
David Pursehouse10829f642015-01-21 11:42:32 +0900115"`403 Forbidden`" is returned if the operation is not allowed because the
David Pursehouse62fe0402014-05-01 17:14:35 +0900116calling user does not have sufficient permissions.
Edwin Kempine026a7d2013-03-13 11:03:42 +0100117
118E.g. some REST endpoints require that the calling user has certain
David Pursehouse8becc2a2013-04-23 18:51:04 +0900119link:access-control.html#global_capabilities[global capabilities]
Edwin Kempine026a7d2013-03-13 11:03:42 +0100120assigned.
121
David Pursehouse10829f642015-01-21 11:42:32 +0900122"`403 Forbidden`" is also returned if `self` is used as account ID and the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100123REST call was done without authentication.
124
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800125==== 404 Not Found
David Pursehouse10829f642015-01-21 11:42:32 +0900126"`404 Not Found`" is returned if the resource that is specified by the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100127URL is not found or is not visible to the calling user. A resource
128cannot be found if the URL contains a non-existing ID or view.
129
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800130==== 405 Method Not Allowed
David Pursehouse10829f642015-01-21 11:42:32 +0900131"`405 Method Not Allowed`" is returned if the resource exists but doesn't
Edwin Kempine026a7d2013-03-13 11:03:42 +0100132support the operation.
133
134E.g. some of the `/groups/` endpoints are only supported for Gerrit
David Pursehouse0ddcb082014-05-01 21:50:18 +0900135internal groups; if they are invoked for an external group the response
David Pursehouse10829f642015-01-21 11:42:32 +0900136is "`405 Method Not Allowed`".
Edwin Kempine026a7d2013-03-13 11:03:42 +0100137
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800138==== 409 Conflict
David Pursehouse10829f642015-01-21 11:42:32 +0900139"`409 Conflict`" is returned if the request cannot be completed because the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100140current state of the resource doesn't allow the operation.
141
142E.g. if you try to submit a change that is abandoned, this fails with
David Pursehouse10829f642015-01-21 11:42:32 +0900143"`409 Conflict`" because the state of the change doesn't allow the submit
Edwin Kempine026a7d2013-03-13 11:03:42 +0100144operation.
145
David Pursehouse10829f642015-01-21 11:42:32 +0900146"`409 Conflict`" is also returned if you try to create a resource but the
Edwin Kempine026a7d2013-03-13 11:03:42 +0100147name is already occupied by an existing resource.
148
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800149==== 412 Precondition Failed
David Pursehouse10829f642015-01-21 11:42:32 +0900150"`412 Precondition Failed`" is returned if a precondition from the request
David Pursehouse62fe0402014-05-01 17:14:35 +0900151header fields is not fulfilled, as described in the link:#preconditions[
Edwin Kempine026a7d2013-03-13 11:03:42 +0100152Preconditions] section.
153
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800154==== 422 Unprocessable Entity
David Pursehouse10829f642015-01-21 11:42:32 +0900155"`422 Unprocessable Entity`" is returned if the ID of a resource that is
Edwin Kempine026a7d2013-03-13 11:03:42 +0100156specified in the request body cannot be resolved.
157
Shawn O. Pearceec9efd72012-04-04 20:44:39 -0700158GERRIT
159------
160Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -0700161
162SEARCHBOX
163---------