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