Convert curl commands to Go code
mholt.github.io
mholt.github.io
Someone also showed me this tool which does the same thing but supports more languages and options: https://shibukawa.github.io/curl_as_dsl/
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.
I already have a small python script that compiles C++ headers into Java interfaces (for services with RPC sync).
More specific to JSON, you may be looking for something like https://mholt.github.io/json-to-go/ (by the same author) or https://github.com/ChimeraCoder/gojson (which I've used myself).
(I find that what I pay in using a static language like Go to manipulate JSON and having to use a tool like that often comes back to me pretty quickly when I take the tool output as a template and start turning all the structs into Real Objects (TM). The dynamic languages are pretty good at taking JSON and yielding a simple pile of dict/map/array/strings/numbers/etc., but if you want to get real objects back out of them the advantage over Go evaporates. Not because the dynamic languages make it "hard", but just because with one of these tools, both Go and the dynamic solutions are roughly as easy.)
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" -d '{"type":"A","name":"www","data":"162.10.66.0","priority":null,"port":null,"weight":null}' "https://api.digitalocean.com/v2/domains/example.com/records"
by hand would seem to indicate you're using the wrong tool for the job. But if other tools use curl commands as a notation for HTTP requests, curl command to programming language conversion has a real use case.APIs aren't the only thing HTTP is used for. Although, you could stick curl commands in shell scripts as well to have dumb API scripts.
There are use cases for every feature.
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).
A generator that could generate libcurl compatible code would have the full power of cURL!
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.
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.
On the other hand, if you leave it off, it will offer gzip to a server and auto uncompress on receipt.
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.
At any rate, thank you for making this; I'm using it with a slack integration right now.
Also, that's not 400 lines of parsing code, that intermixes "parsing" and generation all together.
Mind you Go is not a good language for writing parsers in. (It isn't especially bad, like, say, C, but it lacks almost every conceivable feature that could make parsing code anything other than very, very verbose.)
Basically, by treating the arguments as a stream of tokens, it can be parsed with the same power used for parsing the source of programming language.
Yet for many uses there are natural complexity-levels for option parser tools (like position-independent boolean options, and options taking one argument), hence the many tools for many niches.
And they are easy to write, so everyone writes one.
Or, to parse the curl command string at run time, so it can easily be human-read, as a curl command?
But I guess the idea is to generate a start-point template, which can then be customised for whatever is needed.
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...)
JSON: https://mholt.github.io/json-to-go/
CURL: https://mholt.github.io/curl-to-go/
SQL: https://github.com/knq/xo
The JSON tool especially has saved me a lot of time.
TIL'd.
curl -XPOST httpbin.org/post -d '{"sneaky backtick": "`"}'
produces body := strings.NewReader(`{"sneaky backtick": "`"}`) if err != nil {
// handle err
}
Required, or can you also do: if err {
// handle err
}In practice most Golang errors are handled either:
1) Ignored
2) Logged and swallowed
3) Returned at the point it happened.
4) Fatally exits the program.
One of the "features" of the errors as return codes is that you don't get surprising results that come from the goto style jumps that happen with something like exceptions (unrolling state, etc).
The principal is that errors are values like everything else, and you can write code to handle them however you'd like. See, for instance, the "write" example in this post:
https://blog.golang.org/errors-are-values
There are libraries that people use (sqlx might be an example) that are more like that example than the if-error-return stuff here.