Curl 如何发送带有标头和正文的http数据

Curl 如何发送带有标头和正文的http数据,curl,http-headers,netcat,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:

我使用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界面看到:

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).
请求URL:

POST http://USER_ID:TOKEN_ID@localhost:5555/project/PROJECT_ID
请求标头:

Content-Type: application/json
X-Gitlab-Event: Push Hook
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界面复制的):


据我所知,问题是--head和--data不能一起使用,但根据GitLab的输出,我需要同时使用它们,因为有些东西是header,有些东西是data。我错过了什么?谢谢

您只需要添加HTTP头
内容类型:application/json
&
X-Gitlab-Event:Push-Hook
。您可以使用以下命令指定其他HTTP标头:

curl-H'内容类型:application/json'\
-H'X-Gitlab-Event:推钩'\
-d@body.json“http://USER_ID:TOKEN_ID@localhost:5555/project/project\u ID“
在您的情况下,您已经使用了,它发送一个
HEAD
请求(例如,它只请求响应头)

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

curl-H@headers.txt-d@body.json”http://USER_ID:TOKEN_ID@localhost:5555/project/project\u ID“
使用headers.txt: