javascript - Gitlab 使用 Axios 或 Curl 请求发布/放入 Node - 无法写入资源 - 400 错误

标签 javascript node.js gitlab axios

使用 Gitlab api 时,我似乎无法发布任何内容,除非它是 "Content-Type": "text/plain"。我也尝试过 curl 请求,我可以使用 GET,但也没有成功使用 POST/PUT。

我仔细检查并确保我打开了尽可能公开的访问权限,检查了是否有公钥等。仍然没有成功。

有效

let url = window.djConfig.DJ_URL + "api/v1/git" + "/project/directory";
 axios.put(url, 'stuff',{
  headers: {
    "Content-Type": "text/plain"
   }
  })
  .then(function (res) {
    //do success stuff
  })
  .catch(function (err) {
    //do error stuff
 })

curl --request GET --header 'PRIVATE-TOKEN: ycEKE_uSQiRXzMtrZuZU' 'https://gitlab.example.com/api/v4/git/journey-content/bower%2Ejson?ref=master'

但是如果我尝试做任何更稳健的事情,比如添加提交消息,我会得到错误:

{"message":"Unable to write resource.","status":400}

我有更强大的 Axios 设置

不起作用

let url = window.djConfig.DJ_URL + "api/v1/git" + "/project/directory";
let commit = {
  file_path: 'project/directory',
  branch: 'master',
  content: '{key: "value"}',
  commit_message: 'test commit'
}

axios.put(url, commit,{
   headers: {
    "Content-Type": "application/json"
   }
})
.then(function (res) {
   // do success stuff
})
.catch(function (err) {
  // do error stuff
})


curl --request POST --header 'PRIVATE-TOKEN: ycEKE_uSQiRXzMtrZuZU' 'http://localhost:8911/developerjourney/api/v1/git/journey-content/projectrb%2E?branch=master&author_email=author%40example.com&author_name=Firstname%20Lastname&content=some%20content&commit_message=create%20a%20new%20file'

我喜欢 Gitlab 提供这条无用信息的方式...

If the commit fails for any reason we return a 400 error with a non-specific error message. Possible causes for a failed commit include:

the file_path contained /../ (attempted directory traversal); the new file contents were identical to the current file contents, i.e. the user tried to make an empty commit; the branch was updated by a Git push while the file edit was in progress. Currently gitlab-shell has a boolean return code, preventing GitLab from specifying the error.

不确定我缺少什么,但如果我可以获取但不能发布/放置,感觉就像是权限问题。

更新 如果我使用 --verbose 发出请求,那么这里是终端的完整输出

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8911 (#0)
> POST /developerjourney/api/v1/git/journey-content/projectrb%2E?branch=master&author_email=author%40example.com&author_name=Firstname%20Lastname&content=some%20content&commit_message=create%20a%20new%20file HTTP/1.1
> Host: localhost:8911
> User-Agent: curl/7.54.0
> Accept: */*
> PRIVATE-TOKEN: ycEKE_uSQiRXzMtrZuZU
> 
< HTTP/1.1 400 Bad Request
< X-Powered-By: Express
< X-DJ-Redirect-URL: http://localhost:8911/developerjourney/authenticate
< X-DJ-GitLab-Host: https://localhost:30443
< X-DJ-GitLab-Path: 
< X-DJ-GitLab-Application-Id: 513ce27b4c730d461fa68ceae3eab23f726dc975c3d250de79af8d301f74f4f7
< X-DJ-JourneyGroup: journeys
< X-DJ-URL: http://localhost:8911/developerjourney/
< Set-Cookie: dj-session=18245958cb989b009e7417d76b62331a; Max-Age=1209600; Path=/; Expires=Fri, 05 Jan 2018 19:46:56 GMT; HttpOnly
< Content-Type: application/json; charset=utf-8
< Content-Length: 52
< ETag: W/"34-mpqGlVzme26p5gWU36ILGyMDScQ"
< Date: Fri, 22 Dec 2017 19:46:56 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
{"message":"Unable to write resource.","status":400}

使用 v4 api

如果您使用以下代码段

curl --request POST --header 'PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK' 'http://localhost:8911/developerjourney/api/v4/projects/2/journey-content/app%2Fprojectrb%2E?branch=master&content=some%20content&commit_message=create%20a%20new%20file'

我收到在 HTML 中返回的错误文本。 “journey-content”是我的项目名称,它位于名为“journeys”的组中

最佳答案

您的问题是未使用正确的 API。首先你需要使用/projects api获取项目id

let url = window.djConfig.DJ_URL + "api/v4/" + "projects?owned=true";

然后上面会给你类似下面的东西

[
    {
        "id": XXXXX,
        "description": "",
        "name": "testapi",
        "name_with_namespace": "XXXX / testapi",
        "path": "testapi",
        "path_with_namespace": "XXX/testapi",
        "created_at": "2017-12-24T19:09:11.192Z",
        "default_branch": null,
        "tag_list": [],
        "ssh_url_to_repo": "git@gitlab.com:xxx/testapi.git",
        "http_url_to_repo": "https://gitlab.com/xxx/testapi.git",

您需要从中捕获ID以进行下一个请求

let url = window.djConfig.DJ_URL + "api/v4" + "/projects/<id>/repository/commits";

并且您需要按照以下格式发布数据

{
  "branch": "master",
  "commit_message": "some commit message",
  "actions": [
    {
      "action": "create",
      "file_path": "foo/bar",
      "content": "some content"
    }
  ]
}

它将返回下面的 JSON 响应

[
    {
        "id": "cbf63f8050ed033565e28f8d62288d9f6a8be1a7",
        "short_id": "cbf63f80",
        "title": "Add new file",
        "created_at": "2017-12-24T19:23:34.000+00:00",
        "parent_ids": [],
        "message": "Add new file",
        "author_name": "xxx xxx",
        "author_email": "xxx@xxx.com",
        "authored_date": "2017-12-24T19:23:34.000+00:00",
        "committer_name": "xxx xxx",
        "committer_email": "xxx@xxxx.com",
        "committed_date": "2017-12-24T19:23:34.000+00:00"
    }
]

您甚至可以使用查询参数发布

curl --request POST --header 'PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fprojectrb%2E?branch=master&author_email=author%40example.com&author_name=Firstname%20Lastname&content=some%20content&commit_message=create%20a%20new%20file'

所以您的主要问题是没有在您的网址中使用项目 ID

关于javascript - Gitlab 使用 Axios 或 Curl 请求发布/放入 Node - 无法写入资源 - 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892550/

相关文章:

postgresql - 带有Kubernetes和Gluster的GitLab HA

javascript - 如何使用 Javascript 格式化 HTML5 音频 currentTime 属性

javascript - 如何向尚未添加到页面的元素注册 Javascript 事件处理程序

node.js - nodejs - 如何使用 Node 验证码模块?

html - 如何使用nodejs模块使用xpath提取html内容

javascript - `this` 在全局上下文和内部函数中

git - 我们可以在 docker 镜像中包含 git 命令吗?

gitlab - 将 Nexus Repo 复制到 GitLab 注册表

javascript - 如何添加鼠标悬停音频的延迟(悬停时的声音)

javascript - 获取音频然后将其绘制到 Canvas 上