我如何使用curl来测试Github后接收钩子服务器



Github支持用于通知代码更改的post-receive钩子,文档在这里。现在,为了测试钩子服务器,我需要发布一些json,我想使用curl来这样做。我以前也这样做过,但很少,所以我往往会忘记我的解决方案。

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

Github人员提供了这个JSON文档作为将在参数payload上post的数据示例:

{
  "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 I thought that

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

应该是对curl的正确调用,但是我错了。使用我的示例项目钩子,上面的结果是:

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"rnContent-Type: application/octet-streamrn"}

,给定该端点的动作定义:

class HomeAction < Cramp::Action
  def start
    puts params[:payload]
    render 'thanks'
    finish
  end
end

不太合适。那么,我如何使用curl的命令行版本发布一点JSON作为数据,参数payload ?

我用下面的代码完成了这个工作:

  1. 将json数据保存到文件中。
  2. 删除json文件中的新行。(Vim command= :%s/n//g)
  3. curl --data-urlencode payload@json_data.txt [URL]

似乎提供相同的结果时,张贴到Postbin通过GitHub &旋度。

相关内容

最新更新