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