Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1 | = Gerrit Code Review - REST API Developers' Notes |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 2 | |
| 3 | This document is about developing the REST API. For details of the |
| 4 | actual APIs available in Gerrit, please see the |
| 5 | link:rest-api.html[REST API interface reference]. |
| 6 | |
| 7 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 8 | == Testing REST API Functionality |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 9 | |
| 10 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 11 | === Basic Testing |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 12 | |
| 13 | Basic testing of REST API functionality can be done with `curl`: |
| 14 | |
| 15 | ---- |
| 16 | curl http://localhost:8080/path/to/api/ |
| 17 | ---- |
| 18 | |
| 19 | By default, `curl` sends `GET` requests. To test APIs with `PUT`, `POST`, |
| 20 | or `DELETE`, an additional argument is required: |
| 21 | |
| 22 | ---- |
| 23 | curl -X PUT http://localhost:8080/path/to/api/ |
| 24 | curl -X POST http://localhost:8080/path/to/api/ |
| 25 | curl -X DELETE http://localhost:8080/path/to/api/ |
| 26 | ---- |
| 27 | |
| 28 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 29 | === Sending Data in the Request |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 30 | |
| 31 | Some REST APIs accept data in the request body of `PUT` and `POST` requests. |
| 32 | |
| 33 | Test data can be included from a local file: |
| 34 | |
| 35 | ---- |
| 36 | curl -X PUT -d@testdata.txt --header "Content-Type: application/json" http://localhost:8080/path/to/api/ |
| 37 | ---- |
| 38 | |
Sasa Zivkov | 5449379 | 2013-05-29 16:43:35 +0200 | [diff] [blame] | 39 | Note that the `-d` option will remove the newlines from the content of the |
| 40 | local file. If the content should be sent as-is then use the `--data-binary` |
| 41 | option instead: |
| 42 | |
| 43 | ---- |
| 44 | curl -X PUT --data-binary @testdata.txt --header "Content-Type: text/plain" http://localhost:8080/path/to/api/ |
| 45 | ---- |
| 46 | |
Khai Do | 990ec6a | 2014-07-10 13:15:59 -0700 | [diff] [blame] | 47 | Example to set a Gerrit project's link:rest-api-projects.html#set-project-description[description]: |
| 48 | |
| 49 | ---- |
Han-Wen Nienhuys | 84d830b | 2017-02-15 16:36:04 +0100 | [diff] [blame] | 50 | curl -X PUT --user john:2LlAB3K9B0PF --data-binary @project-desc.txt --header "Content-Type: application/json; charset=UTF-8" http://localhost:8080/a/projects/myproject/description |
Khai Do | 990ec6a | 2014-07-10 13:15:59 -0700 | [diff] [blame] | 51 | ---- |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 52 | |
Edwin Kempin | 427c7d8 | 2021-02-26 12:16:20 +0100 | [diff] [blame] | 53 | [[pretty-json]] |
| 54 | === Pretty JSON |
| 55 | |
| 56 | By default any JSON in responses is compacted. To get pretty-printed JSON add `pp=1` to the request. |
| 57 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 58 | === Authentication |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 59 | |
| 60 | To test APIs that require authentication, the username and password must be specified on |
| 61 | the command line: |
| 62 | |
| 63 | ---- |
Han-Wen Nienhuys | 84d830b | 2017-02-15 16:36:04 +0100 | [diff] [blame] | 64 | curl --user username:password http://localhost:8080/a/path/to/api/ |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 65 | ---- |
| 66 | |
| 67 | This makes it easy to switch users for testing of permissions. |
| 68 | |
| 69 | It is also possible to test with a username and password from the `.netrc` |
| 70 | file (on Windows, `_netrc`): |
| 71 | |
| 72 | ---- |
Han-Wen Nienhuys | 84d830b | 2017-02-15 16:36:04 +0100 | [diff] [blame] | 73 | curl -n http://localhost:8080/a/path/to/api/ |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 74 | ---- |
| 75 | |
| 76 | In both cases, the password should be the user's link:user-upload.html#http[HTTP password]. |
| 77 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 78 | === Verifying Header Content |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 79 | |
| 80 | To verify the headers returned from a REST API call, use `curl` in verbose mode: |
| 81 | |
| 82 | ---- |
Han-Wen Nienhuys | 84d830b | 2017-02-15 16:36:04 +0100 | [diff] [blame] | 83 | curl -v -n -X DELETE http://localhost:8080/a/path/to/api/ |
David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 84 | ---- |
| 85 | |
| 86 | The headers on both the request and the response will be printed. |
| 87 | |
| 88 | |
| 89 | GERRIT |
| 90 | ------ |
| 91 | Part of link:index.html[Gerrit Code Review] |
| 92 | |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 93 | SEARCHBOX |
| 94 | --------- |