| 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 | |
| David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 47 | |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame^] | 48 | === Authentication |
| David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 49 | |
| 50 | To test APIs that require authentication, the username and password must be specified on |
| 51 | the command line: |
| 52 | |
| 53 | ---- |
| 54 | curl --digest --user username:password http://localhost:8080/a/path/to/api/ |
| 55 | ---- |
| 56 | |
| 57 | This makes it easy to switch users for testing of permissions. |
| 58 | |
| 59 | It is also possible to test with a username and password from the `.netrc` |
| 60 | file (on Windows, `_netrc`): |
| 61 | |
| 62 | ---- |
| 63 | curl --digest -n http://localhost:8080/a/path/to/api/ |
| 64 | ---- |
| 65 | |
| 66 | In both cases, the password should be the user's link:user-upload.html#http[HTTP password]. |
| 67 | |
| 68 | |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame^] | 69 | === Verifying Header Content |
| David Pursehouse | ed32132 | 2013-05-17 13:53:32 +0100 | [diff] [blame] | 70 | |
| 71 | To verify the headers returned from a REST API call, use `curl` in verbose mode: |
| 72 | |
| 73 | ---- |
| 74 | curl -v -n --digest -X DELETE http://localhost:8080/a/path/to/api/ |
| 75 | ---- |
| 76 | |
| 77 | The headers on both the request and the response will be printed. |
| 78 | |
| 79 | |
| 80 | GERRIT |
| 81 | ------ |
| 82 | Part of link:index.html[Gerrit Code Review] |
| 83 | |
| Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 84 | SEARCHBOX |
| 85 | --------- |