blob: ec4b66613a4b7510fa704f3269e7aed3b6dd7b6e [file] [log] [blame]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001= Gerrit Code Review - REST API Developers' Notes
David Pursehouseed321322013-05-17 13:53:32 +01002
3This document is about developing the REST API. For details of the
4actual APIs available in Gerrit, please see the
5link:rest-api.html[REST API interface reference].
6
7
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08008== Testing REST API Functionality
David Pursehouseed321322013-05-17 13:53:32 +01009
10
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080011=== Basic Testing
David Pursehouseed321322013-05-17 13:53:32 +010012
13Basic testing of REST API functionality can be done with `curl`:
14
15----
16 curl http://localhost:8080/path/to/api/
17----
18
19By default, `curl` sends `GET` requests. To test APIs with `PUT`, `POST`,
20or `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' Wang61698b12013-12-20 12:55:51 -080029=== Sending Data in the Request
David Pursehouseed321322013-05-17 13:53:32 +010030
31Some REST APIs accept data in the request body of `PUT` and `POST` requests.
32
33Test 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 Zivkov54493792013-05-29 16:43:35 +020039Note that the `-d` option will remove the newlines from the content of the
40local file. If the content should be sent as-is then use the `--data-binary`
41option 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 Do990ec6a2014-07-10 13:15:59 -070047Example to set a Gerrit project's link:rest-api-projects.html#set-project-description[description]:
48
49----
50 curl -X PUT --digest --user john:2LlAB3K9B0PF --data-binary @project-desc.txt --header "Content-Type: application/json;charset=UTF-8" http://localhost:8080/a/projects/myproject/description
51----
David Pursehouseed321322013-05-17 13:53:32 +010052
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080053=== Authentication
David Pursehouseed321322013-05-17 13:53:32 +010054
55To test APIs that require authentication, the username and password must be specified on
56the command line:
57
58----
59 curl --digest --user username:password http://localhost:8080/a/path/to/api/
60----
61
62This makes it easy to switch users for testing of permissions.
63
64It is also possible to test with a username and password from the `.netrc`
65file (on Windows, `_netrc`):
66
67----
68 curl --digest -n http://localhost:8080/a/path/to/api/
69----
70
71In both cases, the password should be the user's link:user-upload.html#http[HTTP password].
72
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080073=== Verifying Header Content
David Pursehouseed321322013-05-17 13:53:32 +010074
75To verify the headers returned from a REST API call, use `curl` in verbose mode:
76
77----
78 curl -v -n --digest -X DELETE http://localhost:8080/a/path/to/api/
79----
80
81The headers on both the request and the response will be printed.
82
83
84GERRIT
85------
86Part of link:index.html[Gerrit Code Review]
87
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -070088SEARCHBOX
89---------