如何使用标题和正文ia curl发送http数据

时间:2018-07-16 15:22:42

标签: curl http-headers netcat

我已经使用netcat捕获了以下数据

root@gitlab_new:/curl_hacks# nc -lp 5555
POST /project/PROJECT_ID HTTP/1.1
Content-Type: application/json
X-Gitlab-Event: Push Hook
Authorization: Basic VVNFUl9JRDpUT0tFTl9JRA==
Connection: close
Host: localhost:5555
Content-Length: 1968

{"object_kind":"push","event_name":"push","before":"87257922812b7ce1e84f2ff4a74f9548e7b521ff","after":"84f95fb521fe48b357b6556f755b177fc9793d6c","ref":"refs/heads/master","checkout_sha":"84f95fb521fe48b357b6556f755b177fc9793d6c","message":null,"user_id":1,"user_name":"Administrator","user_username":"root","user_email":"admin@example.com","user_avatar":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon","project_id":1,"project":{"id":1,"name":"hello-world","description":"","web_url":"http://gitlab_new/root/hello-world","avatar_url":null,"git_ssh_url":"git@gitlab_new:root/hello-world.git","git_http_url":"http://gitlab_new/root/hello-world.git","namespace":"root","visibility_level":0,"path_with_namespace":"root/hello-world","default_branch":"master","ci_config_path":null,"homepage":"http://gitlab_new/root/hello-world","url":"git@gitlab_new:root/hello-world.git","ssh_url":"git@gitlab_new:root/hello-world.git","http_url":"http://gitlab_new/root/hello-world.git"},"commits":[{"id":"84f95fb521fe48b357b6556f755b177fc9793d6c","message":" M commit_history\n","timestamp":"2018-07-16T16:03:39+02:00","url":"http://gitlab_new/root/hello-world/commit/84f95fb521fe48b357b6556f755b177fc9793d6c","author":{"name":"wakatana","email":"wakatana@topsecretmail.gov"},"added":[],"modified":["commit_history"],"removed":[]},{"id":"87257922812b7ce1e84f2ff4a74f9548e7b521ff","message":"1 commit\n","timestamp":"2018-07-16T12:55:13+02:00","url":"http://gitlab_new/root/hello-world/commit/87257922812b7ce1e84f2ff4a74f9548e7b521ff","author":{"name":"wakatana","email":"wakatana@topsecretmail.gov"},"added":["commit_history"],"modified":[],"removed":[]}],"total_commits_count":2,"repository":{"name":"hello-world","url":"git@gitlab_new:root/hello-world.git","description":"","homepage":"http://gitlab_new/root/hello-world","git_http_url":"http://gitlab_new/root/hello-world.git","git_ssh_url":"git@gitlab_new:root/hello-world.git","visibility_level":0}}root@gitlab_new:/curl_hacks#

这些数据是通过GitLab发送的。在这里,它们以(更漂亮)的形式显示,我可以通过GitLab Web界面看到它:

请求网址:

POST http://USER_ID:TOKEN_ID@localhost:5555/project/PROJECT_ID

请求标头:

Content-Type: application/json
X-Gitlab-Event: Push Hook

请求正文:

{
  "object_kind": "push",
  "event_name": "push",
  "before": "87257922812b7ce1e84f2ff4a74f9548e7b521ff",
  "after": "84f95fb521fe48b357b6556f755b177fc9793d6c",
  "ref": "refs/heads/master",
  "checkout_sha": "84f95fb521fe48b357b6556f755b177fc9793d6c",
  "message": null,
  "user_id": 1,
  "user_name": "Administrator",
  "user_username": "root",
  "user_email": "admin@example.com",
  "user_avatar": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  "project_id": 1,
  "project": {
    "id": 1,
    "name": "hello-world",
    "description": "",
    "web_url": "http://gitlab_new/root/hello-world",
    "avatar_url": null,
    "git_ssh_url": "git@gitlab_new:root/hello-world.git",
    "git_http_url": "http://gitlab_new/root/hello-world.git",
    "namespace": "root",
    "visibility_level": 0,
    "path_with_namespace": "root/hello-world",
    "default_branch": "master",
    "ci_config_path": null,
    "homepage": "http://gitlab_new/root/hello-world",
    "url": "git@gitlab_new:root/hello-world.git",
    "ssh_url": "git@gitlab_new:root/hello-world.git",
    "http_url": "http://gitlab_new/root/hello-world.git"
  },
  "commits": [
    {
      "id": "84f95fb521fe48b357b6556f755b177fc9793d6c",
      "message": " M commit_history\n",
      "timestamp": "2018-07-16T16:03:39+02:00",
      "url": "http://gitlab_new/root/hello-world/commit/84f95fb521fe48b357b6556f755b177fc9793d6c",
      "author": {
        "name": "wakatana",
        "email": "wakatana@topsecretmail.gov"
      },
      "added": [

      ],
      "modified": [
        "commit_history"
      ],
      "removed": [

      ]
    },
    {
      "id": "87257922812b7ce1e84f2ff4a74f9548e7b521ff",
      "message": "1 commit\n",
      "timestamp": "2018-07-16T12:55:13+02:00",
      "url": "http://gitlab_new/root/hello-world/commit/87257922812b7ce1e84f2ff4a74f9548e7b521ff",
      "author": {
        "name": "wakatana",
        "email": "wakatana@topsecretmail.gov"
      },
      "added": [
        "commit_history"
      ],
      "modified": [

      ],
      "removed": [

      ]
    }
  ],
  "total_commits_count": 2,
  "repository": {
    "name": "hello-world",
    "url": "git@gitlab_new:root/hello-world.git",
    "description": "",
    "homepage": "http://gitlab_new/root/hello-world",
    "git_http_url": "http://gitlab_new/root/hello-world.git",
    "git_ssh_url": "git@gitlab_new:root/hello-world.git",
    "visibility_level": 0
  }
}

如何使用curl重新发送这些数据?我已经尝试过以下操作(将header.json和body.json从GitLab Web界面复制):

root@lubuntu18-64:~/curl_hacks# curl --head @header.json --data @body.json http://USER_ID:TOKEN_ID@localhost:5555/project/PROJECT_ID
Warning: You can only select one HTTP request method! You asked for both POST
Warning: (-d, --data) and HEAD (-I, --head).

据我了解,问题是--head和--data不能一起使用,但是根据GitLab的输出,我需要同时使用它们,因为某些东西是标头,而某些东西是数据。我缺少什么?谢谢

1 个答案:

答案 0 :(得分:1)

您只需要添加HTTP标头Content-Type: application/jsonX-Gitlab-Event: Push Hook。您可以使用-H or --header指定其他HTTP标头:

curl -H 'Content-Type: application/json' \
     -H 'X-Gitlab-Event: Push Hook' \
     -d @body.json "http://USER_ID:TOKEN_ID@localhost:5555/project/PROJECT_ID"

在您的情况下,您使用过--head which is equivalent to -I,它会发送一个HEAD请求(例如,它仅请求响应头)

从curl 7.55.0开始,您可以为HTTP标头指定一个文件,每行上都有标头条目(但不是json):

curl -H @headers.txt -d @body.json "http://USER_ID:TOKEN_ID@localhost:5555/project/PROJECT_ID"

with headers.txt:

Content-Type: application/json
X-Gitlab-Event: Push Hook