简体   繁体   English

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

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

When using the Gitlab api, I cannot seem to get anything to post unless its of "Content-Type": "text/plain" . 当使用Gitlab api时,我似乎无法发布任何内容,除非它的"Content-Type": "text/plain" I have tried a curl request as well, I can use GET, but also no luck with POST/PUT. 我也尝试了一个curl请求,我可以使用GET,但也没有运气POST / PUT。

I went through and made sure I opened access to as public as possible, checked there was a public key, etc. still no luck. 我经历了并确保尽可能公开访问,检查是否有公钥,等等仍然没有运气。

Works 作品

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'

But if I try to do anything a bit more robust like adding a commit message I get the error: 但是,如果我尝试做任何更强大的事情,比如添加提交消息,我会得到错误:

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

I have my more robust Axios set up as such 我有更强大的Axios设置

Doesn't work 不行

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'

I like how Gitlab provides this useless piece of information... 我喜欢Gitlab如何提供这个无用的信息......

If the commit fails for any reason we return a 400 error with a non-specific error message. 如果提交因任何原因失败,我们将返回400错误,并显示非特定错误消息。 Possible causes for a failed commit include: 提交失败的可能原因包括:

the file_path contained /../ (attempted directory traversal); file_path包含/../(尝试目录遍历); the new file contents were identical to the current file contents, ie the user tried to make an empty commit; 新文件内容与当前文件内容相同,即用户尝试进行空提交; the branch was updated by a Git push while the file edit was in progress. 文件编辑正在进行时,分支由Git push更新。 Currently gitlab-shell has a boolean return code, preventing GitLab from specifying the error. 目前gitlab-shell有一个布尔返回码,阻止GitLab指定错误。

Not sure what I am missing, but its feeling like permissions issue if I can get but not post/put. 不知道我错过了什么,但如果我能得到但不发布/放置,感觉就像权限一样。

update Here is the full out put from the terminal if I make the request with --verbose 更新这里是充满了从终端投入,如果我做与--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}

using v4 api 使用v4 api

If you use the following snippet 如果您使用以下代码段

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'

I get error text returned inside HTML. 我在HTML中返回错误文本。 "journey-content" is my project name which is inside a group called "journeys" “旅程内容”是我的项目名称,在名为“旅程”的组中

Your issue is not using the correct APIs. 您的问题是没有使用正确的API。 First you need to use /projects api to get the project id 首先,您需要使用/projects api来获取项目ID

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

Then above will give you something like below 然后上面会给你一些类似的东西

[
    {
        "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",

You need to capture the ID from this for making the next request 您需要从中捕获ID以进行下一个请求

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

And you need to post the data in below format 您需要以下面的格式发布数据

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

It will return below a JSON response 它将返回低于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"
    }
]

You can even post using query params 您甚至可以使用查询参数发布

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'

So your main issue is not using the project id in your urls 因此,您的主要问题是不在您的网址中使用项目ID

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

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