blob: a28e2301f664fa23ca2c9f418a291a4281c20d5c [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----
Han-Wen Nienhuys84d830b2017-02-15 16:36:04 +010050 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 Do990ec6a2014-07-10 13:15:59 -070051----
David Pursehouseed321322013-05-17 13:53:32 +010052
Edwin Kempin427c7d82021-02-26 12:16:20 +010053[[pretty-json]]
54=== Pretty JSON
55
56By default any JSON in responses is compacted. To get pretty-printed JSON add `pp=1` to the request.
57
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080058=== Authentication
David Pursehouseed321322013-05-17 13:53:32 +010059
60To test APIs that require authentication, the username and password must be specified on
61the command line:
62
63----
Han-Wen Nienhuys84d830b2017-02-15 16:36:04 +010064 curl --user username:password http://localhost:8080/a/path/to/api/
David Pursehouseed321322013-05-17 13:53:32 +010065----
66
67This makes it easy to switch users for testing of permissions.
68
69It is also possible to test with a username and password from the `.netrc`
70file (on Windows, `_netrc`):
71
72----
Han-Wen Nienhuys84d830b2017-02-15 16:36:04 +010073 curl -n http://localhost:8080/a/path/to/api/
David Pursehouseed321322013-05-17 13:53:32 +010074----
75
76In both cases, the password should be the user's link:user-upload.html#http[HTTP password].
77
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080078=== Verifying Header Content
David Pursehouseed321322013-05-17 13:53:32 +010079
80To verify the headers returned from a REST API call, use `curl` in verbose mode:
81
82----
Han-Wen Nienhuys84d830b2017-02-15 16:36:04 +010083 curl -v -n -X DELETE http://localhost:8080/a/path/to/api/
David Pursehouseed321322013-05-17 13:53:32 +010084----
85
86The headers on both the request and the response will be printed.
87
88
89GERRIT
90------
91Part of link:index.html[Gerrit Code Review]
92
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -070093SEARCHBOX
94---------