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