Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use curl to test a Github post-receive hook server?

Tags:

post

github

curl

Github supports post-receive hooks for notification of code changes, documented here. Now, to test a hook server I need to post a bit of json and I'd like to use curl to do so. I've done this before but so infrequently that I tend to forget my solution.

As I recall, doing this has been tedious each time.

The Github folks provide this JSON document as an example of the data that will be POSTed on the parameter 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": "[email protected]",
      "name": "defunkt"
    }
  },
  "commits": [
    {
      "id": "41a212ee83ca127e3c8cf465891ab7216a705f59",
      "url": "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
      "author": {
        "email": "[email protected]",
        "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": "[email protected]",
        "name": "Chris Wanstrath"
      },
      "message": "update pricing a tad",
      "timestamp": "2008-02-15T14:36:34-08:00"
    }
  ],
  "after": "de8251ff97ee194a289832576287d6f8ad74e3d0",
  "ref": "refs/heads/master"
}

Saving that as /tmp/example.json I thought that

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

would have been the correct invocation of curl, but I was not correct. Using my example project hooks the above results in:

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"}

which, given the definition of the action for that end-point:

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

    render 'thanks'
    finish
  end
end

is not quite expected. So, how can I use the command-line version of curl to post a bit of JSON as data, on parameter payload?

like image 210
troutwine Avatar asked Nov 18 '11 03:11

troutwine


1 Answers

I managed to make this work with the following:

  1. Save the json data in a file.
  2. Remove new lines from json file. (Vim command= :%s/\n//g)
  3. curl --data-urlencode payload@json_data.txt [URL]

Seemed to provide identical results when posting to Postbin via GitHub & Curl.

like image 193
Jesse O Avatar answered Oct 09 '22 13:10

Jesse O