🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Project snippets > 原文:[https://docs.gitlab.com/ee/api/project_snippets.html](https://docs.gitlab.com/ee/api/project_snippets.html) * [Snippet visibility level](#snippet-visibility-level) * [List snippets](#list-snippets) * [Single snippet](#single-snippet) * [Create new snippet](#create-new-snippet) * [Update snippet](#update-snippet) * [Delete snippet](#delete-snippet) * [Snippet content](#snippet-content) * [Snippet repository file content](#snippet-repository-file-content) * [Get user agent details](#get-user-agent-details) # Project snippets[](#project-snippets "Permalink") ## Snippet visibility level[](#snippet-visibility-level "Permalink") GitLab 中的片段可以是私有的,内部的或公共的. 您可以使用代码段中的`visibility`字段进行设置. 摘要可见性级别的常量为: | visibility | Description | | --- | --- | | `private` | 该代码段仅在代码段创建者中可见 | | `internal` | 所有已登录的用户都可以看到该代码段 | | `public` | 无需任何身份验证即可访问该代码段 | **注意:**从 2019 年 7 月开始,GitLab.com 上的新项目,组和摘要的`` `Internal`可见性''设置被禁用. 使用" `Internal`可见性"设置的现有项目,组和摘录保留此设置. 您可以在[相关问题中](https://gitlab.com/gitlab-org/gitlab/-/issues/12388)阅读有关更改的更多信息. ## List snippets[](#list-snippets "Permalink") 获取项目摘要列表. ``` GET /projects/:id/snippets ``` Parameters: * `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) ## Single snippet[](#single-snippet "Permalink") 获得一个项目片段. ``` GET /projects/:id/snippets/:snippet_id ``` Parameters: * `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) * `snippet_id` (必填)-项目代码段的 ID ``` { "id": 1, "title": "test", "file_name": "add.rb", "description": "Ruby test snippet", "author": { "id": 1, "username": "john_smith", "email": "john@example.com", "name": "John Smith", "state": "active", "created_at": "2012-05-23T08:00:58Z" }, "updated_at": "2012-06-28T10:52:04Z", "created_at": "2012-06-28T10:52:04Z", "project_id": 1, "web_url": "http://example.com/example/example/snippets/1", "raw_url": "http://example.com/example/example/snippets/1/raw" } ``` ## Create new snippet[](#create-new-snippet "Permalink") 创建一个新的项目片段. 用户必须具有创建新代码段的权限. ``` POST /projects/:id/snippets ``` Parameters: * `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) * `title` (required) - The title of a snippet * `file_name` (必填)-代码段文件的名称 * `description` (可选)-代码段的说明 * `content` (必填)-代码段的内容 * `visibility` (必填)-代码段的可见性 请求示例: ``` curl --request POST "https://gitlab.com/api/v4/projects/:id/snippets" \ --header "PRIVATE-TOKEN: <your access token>" \ --header "Content-Type: application/json" \ -d @snippet.json ``` 上面的示例请求中使用的`snippet.json` : ``` { "title" : "Example Snippet Title", "description" : "More verbose snippet description", "file_name" : "example.txt", "content" : "source code \n with multiple lines\n", "visibility" : "private" } ``` ## Update snippet[](#update-snippet "Permalink") 更新现有的项目代码段. 用户必须具有更改现有代码段的权限. ``` PUT /projects/:id/snippets/:snippet_id ``` Parameters: * `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) * `snippet_id` (必填)-项目代码段的 ID * `title` (可选)-摘要的标题 * `file_name` (可选)-代码段文件的名称 * `description` (可选)-代码段的说明 * `content` (可选)-代码段的内容 * `visibility` (可选)-代码段的可见性 请求示例: ``` curl --request PUT "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id" \ --header "PRIVATE-TOKEN: <your_access_token>" \ --header "Content-Type: application/json" \ -d @snippet.json ``` 上面的示例请求中使用的`snippet.json` : ``` { "title" : "Updated Snippet Title", "description" : "More verbose snippet description", "file_name" : "new_filename.txt", "content" : "updated source code \n with multiple lines\n", "visibility" : "private" } ``` ## Delete snippet[](#delete-snippet "Permalink") 删除现有项目片段. 如果操作成功,则返回`204 No Content`状态代码;如果找不到资源,则返回`404` . ``` DELETE /projects/:id/snippets/:snippet_id ``` Parameters: * `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) * `snippet_id` (必填)-项目代码段的 ID 请求示例: ``` curl --request DELETE "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id" \ --header "PRIVATE-TOKEN: <your_access_token>" ``` ## Snippet content[](#snippet-content "Permalink") 以纯文本形式返回原始项目代码段. ``` GET /projects/:id/snippets/:snippet_id/raw ``` Parameters: * `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) * `snippet_id` (必填)-项目代码段的 ID 请求示例: ``` curl "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id/raw" \ --header "PRIVATE-TOKEN: <your_access_token>" ``` ## Snippet repository file content[](#snippet-repository-file-content "Permalink") 以纯文本形式返回原始文件的内容. ``` GET /projects/:id/snippets/:snippet_id/files/:ref/:file_path/raw ``` Parameters: * `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) * `snippet_id` (必填)-项目代码段的 ID * `ref` (必填)-分支,标记或提交的名称,例如 master * `file_path` (必填)-文件的 URL 编码路径,例如 snippet%2Erb 请求示例: ``` curl "https://gitlab.com/api/v4/projects/1/snippets/2/files/master/snippet%2Erb/raw" \ --header "PRIVATE-TOKEN: <your_access_token>" ``` ## Get user agent details[](#get-user-agent-details "Permalink") 在 GitLab 9.4 中[引入](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/29508) . 仅适用于管理员. ``` GET /projects/:id/snippets/:snippet_id/user_agent_detail ``` | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | Integer | yes | 项目 ID | | `snippet_id` | Integer | yes | 片段的 ID | 请求示例: ``` curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/snippets/2/user_agent_detail" ``` 响应示例: ``` { "user_agent": "AppleWebKit/537.36", "ip_address": "127.0.0.1", "akismet_submitted": false } ```