debian-mirror-gitlab/doc/api/snippets.md

382 lines
10 KiB
Markdown
Raw Normal View History

2017-09-10 17:25:29 +05:30
# Snippets API
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/6373) in GitLab 8.15.
2019-07-07 11:18:12 +05:30
Snippets API operates on [snippets](../user/snippets.md).
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
## Snippet visibility level
2017-08-17 22:00:37 +05:30
Snippets in GitLab can be either private, internal, or public.
You can set it with the `visibility` field in the snippet.
2019-07-07 11:18:12 +05:30
Valid values for snippet visibility levels are:
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
| Visibility | Description |
|:-----------|:----------------------------------------------------|
| `private` | Snippet is visible only to the snippet creator. |
| `internal` | Snippet is visible for any logged in user. |
| `public` | Snippet can be accessed without any authentication. |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
## List all snippets for a user
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Get a list of the current user's snippets.
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
```text
2019-05-30 16:15:17 +05:30
GET /snippets
2017-08-17 22:00:37 +05:30
```
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2019-05-18 00:54:41 +05:30
2020-03-09 13:42:32 +05:30
```shell
2019-07-07 11:18:12 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets
```
Example response:
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
```json
[
{
"id": 42,
"title": "Voluptatem iure ut qui aut et consequatur quaerat.",
"file_name": "mclaughlin.rb",
"description": null,
"visibility": "internal",
"author": {
"id": 22,
"name": "User 0",
"username": "user0",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon",
"web_url": "http://localhost:3000/user0"
},
"updated_at": "2018-09-18T01:12:26.383Z",
"created_at": "2018-09-18T01:12:26.383Z",
"project_id": null,
"web_url": "http://localhost:3000/snippets/42",
"raw_url": "http://localhost:3000/snippets/42/raw"
},
{
"id": 41,
"title": "Ut praesentium non et atque.",
"file_name": "ondrickaemard.rb",
"description": null,
"visibility": "internal",
"author": {
"id": 22,
"name": "User 0",
"username": "user0",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon",
"web_url": "http://localhost:3000/user0"
},
"updated_at": "2018-09-18T01:12:26.360Z",
"created_at": "2018-09-18T01:12:26.360Z",
"project_id": null,
"web_url": "http://localhost:3000/snippets/41",
"raw_url": "http://localhost:3000/snippets/41/raw"
}
]
2019-05-30 16:15:17 +05:30
```
2019-07-07 11:18:12 +05:30
## Get a single snippet
Get a single snippet.
```text
2017-08-17 22:00:37 +05:30
GET /snippets/:id
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------------------|
| `id` | integer | yes | ID of snippet to retrieve. |
Example request:
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
```shell
2019-02-15 15:39:39 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1
2017-08-17 22:00:37 +05:30
```
Example response:
2019-02-15 15:39:39 +05:30
```json
2017-08-17 22:00:37 +05:30
{
"id": 1,
"title": "test",
"file_name": "add.rb",
2017-09-10 17:25:29 +05:30
"description": "Ruby test snippet",
2018-11-08 19:23:39 +05:30
"visibility": "private",
2017-08-17 22:00:37 +05:30
"author": {
"id": 1,
"username": "john_smith",
"email": "john@example.com",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"expires_at": null,
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"web_url": "http://example.com/snippets/1",
}
```
2019-02-15 15:39:39 +05:30
## Single snippet contents
Get a single snippet's raw contents.
2019-07-07 11:18:12 +05:30
```text
2019-02-15 15:39:39 +05:30
GET /snippets/:id/raw
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------------------|
| `id` | integer | yes | ID of snippet to retrieve. |
2019-02-15 15:39:39 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2020-03-09 13:42:32 +05:30
```shell
2019-02-15 15:39:39 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1/raw
```
Example response:
2019-07-07 11:18:12 +05:30
```text
2019-02-15 15:39:39 +05:30
Hello World snippet
```
2017-08-17 22:00:37 +05:30
## Create new snippet
2019-07-07 11:18:12 +05:30
Create a new snippet.
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
NOTE: **Note:**
The user must have permission to create new snippets.
```text
2017-08-17 22:00:37 +05:30
POST /snippets
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:--------------|:-------|:---------|:---------------------------------------------------|
| `title` | string | yes | Title of a snippet. |
| `file_name` | string | yes | Name of a snippet file. |
2019-10-12 21:52:04 +05:30
| `content` | string | yes | Content of a snippet. |
2019-07-07 11:18:12 +05:30
| `description` | string | no | Description of a snippet. |
2019-10-12 21:52:04 +05:30
| `visibility` | string | no | Snippet's [visibility](#snippet-visibility-level). |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
```shell
2018-03-17 18:26:18 +05:30
curl --request POST \
2019-10-12 21:52:04 +05:30
--data '{"title": "This is a snippet", "content": "Hello world", "description": "Hello World snippet", "file_name": "test.txt", "visibility": "internal" }' \
2018-03-17 18:26:18 +05:30
--header 'Content-Type: application/json' \
--header "PRIVATE-TOKEN: valid_api_token" \
https://gitlab.example.com/api/v4/snippets
2017-08-17 22:00:37 +05:30
```
Example response:
2019-02-15 15:39:39 +05:30
```json
2017-08-17 22:00:37 +05:30
{
"id": 1,
"title": "This is a snippet",
"file_name": "test.txt",
2017-09-10 17:25:29 +05:30
"description": "Hello World snippet",
2018-11-08 19:23:39 +05:30
"visibility": "internal",
2017-08-17 22:00:37 +05:30
"author": {
"id": 1,
"username": "john_smith",
"email": "john@example.com",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"expires_at": null,
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"web_url": "http://example.com/snippets/1",
}
```
## Update snippet
2019-07-07 11:18:12 +05:30
Update an existing snippet.
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
NOTE: **Note:**
The user must have permission to change an existing snippet.
```text
2017-08-17 22:00:37 +05:30
PUT /snippets/:id
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:--------------|:--------|:---------|:---------------------------------------------------|
| `id` | integer | yes | ID of snippet to update. |
| `title` | string | no | Title of a snippet. |
| `file_name` | string | no | Name of a snippet file. |
| `description` | string | no | Description of a snippet. |
2019-10-12 21:52:04 +05:30
| `content` | string | no | Content of a snippet. |
2019-07-07 11:18:12 +05:30
| `visibility` | string | no | Snippet's [visibility](#snippet-visibility-level). |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
```shell
2018-03-17 18:26:18 +05:30
curl --request PUT \
2019-10-12 21:52:04 +05:30
--data '{"title": "foo", "content": "bar"}' \
2018-03-17 18:26:18 +05:30
--header 'Content-Type: application/json' \
--header "PRIVATE-TOKEN: valid_api_token" \
https://gitlab.example.com/api/v4/snippets/1
2017-08-17 22:00:37 +05:30
```
Example response:
2019-02-15 15:39:39 +05:30
```json
2017-08-17 22:00:37 +05:30
{
"id": 1,
"title": "test",
"file_name": "add.rb",
2017-09-10 17:25:29 +05:30
"description": "description of snippet",
2018-11-08 19:23:39 +05:30
"visibility": "internal",
2017-08-17 22:00:37 +05:30
"author": {
"id": 1,
"username": "john_smith",
"email": "john@example.com",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"expires_at": null,
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"web_url": "http://example.com/snippets/1",
}
```
## Delete snippet
2019-07-07 11:18:12 +05:30
Delete an existing snippet.
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
```text
2017-08-17 22:00:37 +05:30
DELETE /snippets/:id
```
Parameters:
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:-------------------------|
| `id` | integer | yes | ID of snippet to delete. |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
```shell
2019-02-15 15:39:39 +05:30
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"
2017-08-17 22:00:37 +05:30
```
2019-07-07 11:18:12 +05:30
The following are possible return codes:
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
| Code | Description |
|:------|:--------------------------------------------|
| `204` | Delete was successful. No data is returned. |
| `404` | The snippet wasn't found. |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
## List all public snippets
List all public snippets.
```text
2017-08-17 22:00:37 +05:30
GET /snippets/public
```
2019-07-07 11:18:12 +05:30
Parameters:
| Attribute | Type | Required | Description |
|:-----------|:--------|:---------|:---------------------------------------|
| `per_page` | integer | no | Number of snippets to return per page. |
| `page` | integer | no | Page to retrieve. |
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2020-03-09 13:42:32 +05:30
```shell
2019-02-15 15:39:39 +05:30
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/public?per_page=2&page=1
2017-08-17 22:00:37 +05:30
```
Example response:
2019-02-15 15:39:39 +05:30
```json
2017-08-17 22:00:37 +05:30
[
{
"author": {
"avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
"id": 12,
"name": "Libby Rolfson",
"state": "active",
"username": "elton_wehner",
"web_url": "http://localhost:3000/elton_wehner"
},
"created_at": "2016-11-25T16:53:34.504Z",
"file_name": "oconnerrice.rb",
"id": 49,
"raw_url": "http://localhost:3000/snippets/49/raw",
"title": "Ratione cupiditate et laborum temporibus.",
"updated_at": "2016-11-25T16:53:34.504Z",
"web_url": "http://localhost:3000/snippets/49"
},
{
"author": {
"avatar_url": "http://www.gravatar.com/avatar/36583b28626de71061e6e5a77972c3bd?s=80&d=identicon",
"id": 16,
"name": "Llewellyn Flatley",
"state": "active",
"username": "adaline",
"web_url": "http://localhost:3000/adaline"
},
"created_at": "2016-11-25T16:53:34.479Z",
"file_name": "muellershields.rb",
"id": 48,
"raw_url": "http://localhost:3000/snippets/48/raw",
"title": "Minus similique nesciunt vel fugiat qui ullam sunt.",
"updated_at": "2016-11-25T16:53:34.479Z",
2018-11-08 19:23:39 +05:30
"web_url": "http://localhost:3000/snippets/48",
"visibility": "public"
2017-08-17 22:00:37 +05:30
}
]
```
2017-09-10 17:25:29 +05:30
## Get user agent details
2020-03-09 13:42:32 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/12655) in GitLab 9.4.
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
NOTE: **Note:**
Available only for administrators.
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
```text
2017-09-10 17:25:29 +05:30
GET /snippets/:id/user_agent_detail
```
2019-07-07 11:18:12 +05:30
| Attribute | Type | Required | Description |
|:----------|:--------|:---------|:---------------|
| `id` | integer | yes | ID of snippet. |
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
Example request:
2020-03-09 13:42:32 +05:30
```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/snippets/1/user_agent_detail
2017-09-10 17:25:29 +05:30
```
Example response:
```json
{
"user_agent": "AppleWebKit/537.36",
"ip_address": "127.0.0.1",
"akismet_submitted": false
}
```