繁体   English   中英

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

[英]Gitlab Post/Put in Node with Axios or Curl Request - Unable to write resource - 400 error

当使用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如何提供这个无用的信息......

如果提交因任何原因失败,我们将返回400错误,并显示非特定错误消息。 提交失败的可能原因包括:

file_path包含/../(尝试目录遍历); 新文件内容与当前文件内容相同,即用户尝试进行空提交; 文件编辑正在进行时,分支由Git push更新。 目前gitlab-shell有一个布尔返回码,阻止GitLab指定错误。

不知道我错过了什么,但如果我能得到但不发布/放置,感觉就像权限一样。

更新这里是充满了从终端投入,如果我做与--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中返回错误文本。 “旅程内容”是我的项目名称,在名为“旅程”的组中

您的问题是没有使用正确的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM