Hey HN, I wrote this to scratch a perpetual itch after reading enough API documentations and re-writing basically the same code over and over. Right now it only supports the most common flags that I saw. As far as I'm concerned the project is finished, but PRs are welcome to cover any glaring omissions or fix bugs.<p>Someone also showed me this tool which does the same thing but supports more languages and options: <a href="https://shibukawa.github.io/curl_as_dsl/" rel="nofollow">https://shibukawa.github.io/curl_as_dsl/</a>
Great work, mholt!<p>He also wrote json-to-go[0]--copy/paste JSON and it generates a matching Go struct. This ends up being extremely useful and saves a lot of time for extensive JSON responses.<p>[0]: <a href="https://mholt.github.io/json-to-go/" rel="nofollow">https://mholt.github.io/json-to-go/</a>
This is awesome. Combined with Chrome Developer Tools' "Copy request as cURL" feature, you can reverse engineer some service and turn it into Go code so easily.
Given the connection with Chrome Developer Tools' "Copy request as cURL" I can see the usefulness of this in a general sense; however, I was personally somewhat sad to see that it doesn't actually convert to code that makes use of libcurl.<p>As such, the resulting project is very limited in exactly which cURL commandlines it is capable of converting, and, particularly, it is only able to handle HTTP connections (unless I missed something).<p>A generator that could generate libcurl compatible code would have the full power of cURL!
This is really neat. There's, I don't know if it's a niggle or a bug or an interface issue, but well, let's call it a usability bug.<p>If your copy to curl request in chrome accepts gzip and/or deflate, then your tool will add an "Accept-Encoding" Header. So far so good.<p>In go 1.5.1 at least, from my cursory testing, the runtime will not auto-decompress a bytestream if you've manually specified an accepted encoding.<p>On the other hand, if you leave it off, it will offer gzip to a server and auto uncompress on receipt.<p>I'm still not sure this is a bug, but I definitely commented out my accept encoding line in my sample code because I don't want to manually decompress every response.<p>At any rate, thank you for making this; I'm using it with a slack integration right now.
Another alternative is Postman [0]. Supports C, Java, Go, Ruby, Swift, etc. Available as a Chrome app. Not really the same (doesn't convert from one to another AFAIK), but it gives you a common interface.<p>[0]: <a href="https://www.getpostman.com" rel="nofollow">https://www.getpostman.com</a>
It's not that easy to parse this curl command. He needed 400 lines (<a href="https://github.com/mholt/curl-to-go/blob/gh-pages/resources/js/curl-to-go.js" rel="nofollow">https://github.com/mholt/curl-to-go/blob/gh-pages/resources/...</a>). Most libraries out there do this job, but are focused on parsing from environment variables instead of a string argument, because the programs that needs this usually are CLI programs. That's what I found when doing the parse in ruby which basically has only OptParse. There is an opportunity here. Projects like these that receives a command input are very useful.
\tangent Wouldn't it make sense for this to be a library? Though most of the py looks so simple, it already has the libraries.<p>Or, to parse the curl command string at run time, so it can easily be human-read, as a curl command?<p>But I guess the idea is to generate a start-point template, which can then be customised for whatever is needed.<p>It would be nice for a tool to combine both - so new code is attached to the curl command string... but not very readable... and not very flexible (I bet the next thing you wanted to add would be something that hadn't been considered...)
A couple of these tools have sprung up:<p>JSON: <a href="https://mholt.github.io/json-to-go/" rel="nofollow">https://mholt.github.io/json-to-go/</a><p>CURL: <a href="https://mholt.github.io/curl-to-go/" rel="nofollow">https://mholt.github.io/curl-to-go/</a><p>SQL: <a href="https://github.com/knq/xo" rel="nofollow">https://github.com/knq/xo</a><p>The JSON tool especially has saved me a lot of time.
Go question. Is:<p><pre><code> if err != nil {
// handle err
}
</code></pre>
Required, or can you also do:<p><pre><code> if err {
// handle err
}</code></pre>