views:

267

answers:

2

First of all, I've never really used APIs before, and I've never used the HTTP library in Haskell. I'm not sure what I'm doing wrong here, so maybe somebody who knows can help.

I'm using what I can read of this: http://github.com/defunkt/gist/blob/master/gist.rb, namely the write method, to write this:

req = postRequest "http://gist.github.com/gists/new"

testPost = simpleHTTP $ req {rqBody = urlEncodeVars 
                                      [("login", "Raynes"),
                                       ("token","<removed>"),
                                       ("file_ext[gistfile1]",".hs"),
                                       ("file_name[gistfile1]","testfile"),
                                       ("file_contents[gistfile1]","main = putStrLn \"Hello, world!\"")]}

When ran, testPost gives this output:

Right HTTP/1.1 302 Found 
Server: nginx/0.7.61
Date: Sun, 29 Nov 2009 17:13:51 GMT
Content-Type: text/html; charset=utf-8
Connection: close
Status: 302 Found
Location: http://gist.github.com/gists/new
X-Runtime: 1ms
Content-Length: 98
Set-Cookie: _github_ses=BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D--884981fc5aa85daf318eeff084d98e2cff92578f; path=/; expires=Wed, 01 Jan 2020 08:00:00 GMT; HttpOnly
Cache-Control: no-cache

As far as I know, the Location should be a link to the new Gist. However, no new Gist is made. I'm not sure what I'm doing wrong. There is basically no documentation for the Gist API, and the only thing I can assume is that I'm not translating the Ruby correctly. Like I said, I've never really used the HTTP library before.

Any help is appreciated.

A: 

Your POST url is wrong, according to source is should be http://gist.github.com/gists source

edbond
That was an accident on my part. I was experimenting and forgot to remove that.
Rayne
+1  A: 

I figured it out myself after reading an example of using Network.Browser.browse.

req = "http://gist.github.com/gists"

testPost = do
  (uri, rsp) <- Network.Browser.browse $ do
                  setAllowRedirects True
                  request $ formToRequest $
                          Form POST (fromJust $ parseURI req)
                                   [("file_ext[gistfile1]",".hs"),
                                    ("file_contents[gistfile1]","main = putStrLn \"Hello, world!\""),
                                    ("login","Raynes"),
                                    ("token","removed")]
  return uri
Rayne