TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Convert curl commands to Go code

193 pointsby pythonistover 9 years ago

17 comments

mholtover 9 years ago
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&#x27;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:&#x2F;&#x2F;shibukawa.github.io&#x2F;curl_as_dsl&#x2F;" rel="nofollow">https:&#x2F;&#x2F;shibukawa.github.io&#x2F;curl_as_dsl&#x2F;</a>
评论 #11077510 未加载
travjonesover 9 years ago
Great work, mholt!<p>He also wrote json-to-go[0]--copy&#x2F;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:&#x2F;&#x2F;mholt.github.io&#x2F;json-to-go&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mholt.github.io&#x2F;json-to-go&#x2F;</a>
评论 #11076044 未加载
评论 #11077023 未加载
评论 #11076216 未加载
alpbover 9 years ago
This is awesome. Combined with Chrome Developer Tools&#x27; &quot;Copy request as cURL&quot; feature, you can reverse engineer some service and turn it into Go code so easily.
评论 #11075636 未加载
评论 #11075487 未加载
评论 #11075379 未加载
merlincoreyover 9 years ago
Given the connection with Chrome Developer Tools&#x27; &quot;Copy request as cURL&quot; I can see the usefulness of this in a general sense; however, I was personally somewhat sad to see that it doesn&#x27;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!
vessenesover 9 years ago
This is really neat. There&#x27;s, I don&#x27;t know if it&#x27;s a niggle or a bug or an interface issue, but well, let&#x27;s call it a usability bug.<p>If your copy to curl request in chrome accepts gzip and&#x2F;or deflate, then your tool will add an &quot;Accept-Encoding&quot; 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&#x27;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&#x27;m still not sure this is a bug, but I definitely commented out my accept encoding line in my sample code because I don&#x27;t want to manually decompress every response.<p>At any rate, thank you for making this; I&#x27;m using it with a slack integration right now.
joelg236over 9 years ago
Another alternative is Postman [0]. Supports C, Java, Go, Ruby, Swift, etc. Available as a Chrome app. Not really the same (doesn&#x27;t convert from one to another AFAIK), but it gives you a common interface.<p>[0]: <a href="https:&#x2F;&#x2F;www.getpostman.com" rel="nofollow">https:&#x2F;&#x2F;www.getpostman.com</a>
评论 #11078425 未加载
zobzuover 9 years ago
Note that by default, curl itself can also output C code version of the CLI command as well (yay)
andersonmvdover 9 years ago
It&#x27;s not that easy to parse this curl command. He needed 400 lines (<a href="https:&#x2F;&#x2F;github.com&#x2F;mholt&#x2F;curl-to-go&#x2F;blob&#x2F;gh-pages&#x2F;resources&#x2F;js&#x2F;curl-to-go.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mholt&#x2F;curl-to-go&#x2F;blob&#x2F;gh-pages&#x2F;resources&#x2F;...</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&#x27;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.
评论 #11076258 未加载
hyperpalliumover 9 years ago
\tangent Wouldn&#x27;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&#x27;t been considered...)
lspearsover 9 years ago
A couple of these tools have sprung up:<p>JSON: <a href="https:&#x2F;&#x2F;mholt.github.io&#x2F;json-to-go&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mholt.github.io&#x2F;json-to-go&#x2F;</a><p>CURL: <a href="https:&#x2F;&#x2F;mholt.github.io&#x2F;curl-to-go&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mholt.github.io&#x2F;curl-to-go&#x2F;</a><p>SQL: <a href="https:&#x2F;&#x2F;github.com&#x2F;knq&#x2F;xo" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;knq&#x2F;xo</a><p>The JSON tool especially has saved me a lot of time.
misiti3780over 9 years ago
great idea - i wish this existed for every language !
MasterScratover 9 years ago
Would love the same thing for python!
评论 #11076061 未加载
评论 #11076395 未加载
kilotarasover 9 years ago
Looks like backtics aren&#x27;t sanitized<p><pre><code> curl -XPOST httpbin.org&#x2F;post -d &#x27;{&quot;sneaky backtick&quot;: &quot;`&quot;}&#x27; </code></pre> produces<p><pre><code> body := strings.NewReader(`{&quot;sneaky backtick&quot;: &quot;`&quot;}`)</code></pre>
hyperpalliumover 9 years ago
Super minor point: according to the curl manpage, <i>-d data</i> implies POST, so you don&#x27;t need <i>-X POST</i> in the examples
nodesocketover 9 years ago
Go question. Is:<p><pre><code> if err != nil { &#x2F;&#x2F; handle err } </code></pre> Required, or can you also do:<p><pre><code> if err { &#x2F;&#x2F; handle err }</code></pre>
评论 #11078451 未加载
sylvinusover 9 years ago
Nice! Small nitpick: &quot;-A&quot; doesn&#x27;t seem to be supported.
评论 #11075551 未加载
dkarapetyanover 9 years ago
Go is really verbose but this is really great.
评论 #11076819 未加载