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