blob: c0dde1c71e0f18499cd71baf13b288a5f28c1214 [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---------
12link: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
20link:rest-api-projects.html[/projects/]::
21 Project related REST endpoints
22
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070023Protocol Details
24----------------
25
Edwin Kempind089fc42012-07-18 14:58:40 +020026[[authentication]]
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070027Authentication
28~~~~~~~~~~~~~~
29By default all REST endpoints assume anonymous access and filter
30results to correspond to what anonymous users can read (which may
31be nothing at all).
32
33Users (and programs) may authenticate using HTTP authentication by
34supplying the HTTP password from the user's account settings page.
35Gerrit by default uses HTTP digest authentication. To authenticate,
36prefix the endpoint URL with `/a/`. For example to authenticate to
37`/projects/` request URL `/a/projects/`.
38
Shawn Pearcea90a43a2013-01-26 12:50:56 -080039[[preconditions]]
40Preconditions
41~~~~~~~~~~~~~
42Clients can request PUT to create a new resource and not overwrite
43an existing one by adding `If-None-Match: *` to the request HTTP
44headers. If the named resource already exists the server will respond
45with HTTP 412 Precondition Failed.
46
Edwin Kempind089fc42012-07-18 14:58:40 +020047[[output]]
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070048Output Format
49~~~~~~~~~~~~~
Shawn O. Pearceea6d0b52012-11-16 10:57:31 -080050Most APIs return pretty printed JSON by default. Compact JSON can be
51requested by setting the `Accept` HTTP request header to include
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070052`application/json`, for example:
53
54----
55 GET /projects/ HTTP/1.0
56 Accept: application/json
57----
58
59JSON responses are encoded using UTF-8 and use content type
David Pursehouse221d4f62012-06-08 17:38:08 +090060`application/json`. The JSON response body starts with a magic prefix
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070061line that must be stripped before feeding the rest of the response
62body to a JSON parser:
63
64----
65 )]}'
66 [ ... valid JSON ... ]
67----
68
Shawn O. Pearceea6d0b52012-11-16 10:57:31 -080069The default JSON format is pretty, which uses extra whitespace to make
70the output more readable for a human. Producing (and parsing) the
71non-pretty compact format is more efficient so tools should request it
72by using the `Accept: application/json` header or `pp=0` query
73parameter whenever possible.
Shawn O. Pearceec9efd72012-04-04 20:44:39 -070074
75Responses will be gzip compressed by the server if the HTTP
76`Accept-Encoding` request header is set to `gzip`. This may
77save on network transfer time for larger responses.
78
Edwin Kempine3446292013-02-19 16:40:14 +010079[[timestamp]]
80Timestamp
81~~~~~~~~~
82Timestamps are given in UTC and have the format
83"'yyyy-mm-dd hh:mm:ss.fffffffff'" where "'ffffffffff'" indicates the
84nanoseconds.
85
Edwin Kempin7fe2f7f2013-02-06 10:12:52 +010086[[encoding]]
87Encoding
88~~~~~~~~
89All IDs that appear in the URL of a REST call (e.g. project name, group name)
90must be URL encoded.
91
Edwin Kempine026a7d2013-03-13 11:03:42 +010092[[response-codes]]
93Response Codes
94~~~~~~~~~~~~~~
95HTTP status codes are well defined and the Gerrit REST endpoints use
96them as described in the HTTP spec.
97
98Here are examples for some HTTP status codes that show how they are
99used in the context of the Gerrit REST API.
100
101400 Bad Request
102^^^^^^^^^^^^^^^
103`400 Bad Request` is used if the request is not understood by the
104server due to malformed syntax.
105
106E.g. `400 Bad Request` is returned if JSON input is expected but the
107'Content-Type' of the request is not 'application/json' or the request
108body doesn't contain valid JSON.
109
110`400 Bad Request` is also used if required input fields are not set or
111if options are set which cannot be used together.
112
113403 Forbidden
114^^^^^^^^^^^^^
115`403 Forbidden` is used if the operation is not allowed because the
116calling user has no sufficient permissions.
117
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
122`403 Forbidden` is also used if `self` is used as account ID and the
123REST call was done without authentication.
124
125404 Not Found
126^^^^^^^^^^^^^
127`404 Not Found` is returned if the resource that is specified by the
128URL is not found or is not visible to the calling user. A resource
129cannot be found if the URL contains a non-existing ID or view.
130
131405 Method Not Allowed
132^^^^^^^^^^^^^^^^^^^^^^
133`405 Method Not Allowed` is used if the resource exists but doesn't
134support the operation.
135
136E.g. some of the `/groups/` endpoints are only supported for Gerrit
137internal groups, if they are invoked for an external group the response
138is `405 Method Not Allowed`.
139
140409 Conflict
141^^^^^^^^^^^^
142`409 Conflict` is used if the request cannot be completed because the
143current state of the resource doesn't allow the operation.
144
145E.g. if you try to submit a change that is abandoned, this fails with
146`409 Conflict` because the state of the change doesn't allow the submit
147operation.
148
149`409 Conflict` is also used if you try to create a resource but the
150name is already occupied by an existing resource.
151
152412 Precondition Failed
153^^^^^^^^^^^^^^^^^^^^^^^
154`412 Precondition Failed` is used if a precondition from the request
155header fields is not fulfilled as described in the link:#preconditions[
156Preconditions] section.
157
158422 Unprocessable Entity
159^^^^^^^^^^^^^^^^^^^^^^^^
160`422 Unprocessable Entity` is returned if the ID of a resource that is
161specified in the request body cannot be resolved.
162
Shawn O. Pearceec9efd72012-04-04 20:44:39 -0700163GERRIT
164------
165Part of link:index.html[Gerrit Code Review]