如何使用curl测试Github后接收挂钩服务器?

时间:2011-11-18 03:24:08

标签: post curl github

Github支持用于代码更改通知的post-receive挂钩,记录为here。现在,为了测试一个钩子服务器,我需要发布一些json,我想使用curl这样做。我以前做过这个,但很少,我倾向于忘记我的解决方案。

我记得,每次这样做都很乏味。

Github人员提供此JSON文档作为将在参数payload上发布的数据的示例:

{
  "before": "5aef35982fb2d34e9d9d4502f6ede1072793222d",
  "repository": {
    "url": "http://github.com/defunkt/github",
    "name": "github",
    "description": "You're lookin' at it.",
    "watchers": 5,
    "forks": 2,
    "private": 1,
    "owner": {
      "email": "chris@ozmm.org",
      "name": "defunkt"
    }
  },
  "commits": [
    {
      "id": "41a212ee83ca127e3c8cf465891ab7216a705f59",
      "url": "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
      "author": {
        "email": "chris@ozmm.org",
        "name": "Chris Wanstrath"
      },
      "message": "okay i give in",
      "timestamp": "2008-02-15T14:57:17-08:00",
      "added": ["filepath.rb"]
    },
    {
      "id": "de8251ff97ee194a289832576287d6f8ad74e3d0",
      "url": "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
      "author": {
        "email": "chris@ozmm.org",
        "name": "Chris Wanstrath"
      },
      "message": "update pricing a tad",
      "timestamp": "2008-02-15T14:36:34-08:00"
    }
  ],
  "after": "de8251ff97ee194a289832576287d6f8ad74e3d0",
  "ref": "refs/heads/master"
}

将其保存为/tmp/example.json认为

 $ curl -XPOST -F "payload=@/tmp/example.json" http://localhost:3000/

本来是curl的正确调用,但我不对。使用我的示例项目hooks,结果如下:

127.0.0.1 - - [17/Nov/2011 22:20:51] "POST  HTTP/1.1" 200 - 0.0295
{:filename=>"example.json", :type=>"application/octet-stream", :name=>"payload", :tempfile=>#<File:/tmp/RackMultipart20111117-11639-1ecu4i1>, :head=>"Content-Disposition: form-data; name=\"payload\"; filename=\"example.json\"\r\nContent-Type: application/octet-stream\r\n"}

,给定该终点的行动定义:

class HomeAction < Cramp::Action
  def start
    puts params[:payload]

    render 'thanks'
    finish
  end
end

不太令人期待。那么,如何使用命令行版本的curl在参数payload上发布一些JSON作为数据?

1 个答案:

答案 0 :(得分:6)

我设法使用以下内容完成此工作:

  1. 将json数据保存在文件中。
  2. 从json文件中删除新行。 (Vim command = :%s/\n//g
  3. curl --data-urlencode payload@json_data.txt [URL]
  4. 通过GitHub&amp;发布到Postbin时,似乎提供相同的结果卷曲。