Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Gitlab Post/Put in节点具有Axios或Curl请求-无法写入资源-400错误_Javascript_Node.js_Gitlab_Axios - Fatal编程技术网

Javascript Gitlab Post/Put in节点具有Axios或Curl请求-无法写入资源-400错误

Javascript Gitlab Post/Put in节点具有Axios或Curl请求-无法写入资源-400错误,javascript,node.js,gitlab,axios,Javascript,Node.js,Gitlab,Axios,当使用Gitlab api时,我似乎无法获得任何要发布的内容,除非它的“内容类型”:“text/plain”。我也尝试了一个卷发请求,我可以使用GET,但是POST/PUT也没有成功 我检查了一下,确保我打开了尽可能公开的访问权限,检查了是否有公钥,等等。仍然没有运气 有效 let url = window.djConfig.DJ_URL + "api/v1/git" + "/project/directory"; axios.put(url, 'stuff',{ headers: {

当使用Gitlab api时,我似乎无法获得任何要发布的内容,除非它的
“内容类型”:“text/plain”
。我也尝试了一个卷发请求,我可以使用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错误,其中包含 非特定错误消息。提交失败的可能原因 包括:

包含/。/(尝试的目录遍历)的文件路径;新的 文件内容与当前文件内容相同,即 用户试图进行空提交;该分支已由Git更新 在文件编辑过程中推送。目前gitlab shell有一个 布尔返回代码,防止GitLab指定错误

不确定我遗漏了什么,但如果我可以获取但不能发布/发布,感觉就像权限问题一样

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


我在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";
它将在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'

因此,您的主要问题是没有在URL中使用项目id

共享您的代码。@cgTag希望这会有所帮助-我已经添加了它的实现方式。如果使用
curl
,会发生什么?我认为您应该发布您的请求和响应标题。@JPSilvashy-标题图像已添加到post@JPSilvashy因此,如果我尝试推送任何非纯文本/纯文本的内容类型,这似乎是一个问题。因此,我现在将重写所有内容。这是有道理的,我不知道为什么文档中没有对此进行预先解释。太好了,我明天会给它一个测试!我一直在努力让它工作,我似乎得到了axios和旋度的500错误。使用curl时,当我使用'projects/:id'时,我得到了500个错误。我假设这可能与我使用的api版本有关?这是v1。您需要使用v4使用v4,您的示例在HTML中返回错误文本。查看我的问题以获取更新信息。因此,我能够正确地调用Gitlab.com,我现在的印象是,这与Gitlab的node应用程序和docker实例之间的通信有关。
{
  "branch": "master",
  "commit_message": "some commit message",
  "actions": [
    {
      "action": "create",
      "file_path": "foo/bar",
      "content": "some content"
    }
  ]
}
[
    {
        "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'