top | item 30642281

Convert curl commands to code in several languages

262 points| djha-skin | 4 years ago |curlconverter.com

86 comments

order
[+] NickC_dev|4 years ago|reply
Original author here. Many smart people have contributed code over the years, but one warrants special mention.

About a year ago, verhovsky showed up out of nowhere. He rewrote the core of the application and increased the professionalism across the board. (dedicated domain, github page hosting, UI refresh, privacy improvements, and much more)

The tree-sitter PR is a monster achievement: https://github.com/curlconverter/curlconverter/pull/278

Search for parseAnsiCString in there. I don't think that had ever been implemented in JavaScript before.

For you, verhovsky, 10x engineer might be an understatement. Thank you!

[+] DyslexicAtheist|4 years ago|reply
how about C ... after all curl is just a frontend to libcurl?
[+] protoman3000|4 years ago|reply
Anybody else having reached the point of language indifference/boredom?

Everything I everywhere now see is just the same: Gluecode, library calls, unnecessary verbosity, Blackbox inside Blackbox etc.

And this project slaps it again into my face, with every example it’s literally always the same - only that language uses a colon, the other a semicolon, the other catches exceptions with ? etc. but in a way it’s all just the same.

How to overcome this?

I find, only pointfree notation has some beauty to it that makes reading code sweet again.

[+] capableweb|4 years ago|reply
> How to overcome this?

You start with branching out to actually different programming languages. If you already know PHP, don't go and learn Java or C#, you basically already know those from PHP, you're only missing knowledge of the public API (what methods should be called when, naming conventions) and so on.

Instead, seek languages that when you see them the first time you say "wow, I don't understand anything" and sit down and try to learn one of those. For me, Clojure was always one of those languages that kindled the "what the hell is that" feeling in me, but after reading a lot of praise for the language, I started learning it anyways.

Now I work on Clojure(Script) code full-time, and programming is finally a fun thing to do again.

I won't try to learn Common Lisp or anything like that next, as I know that if I spent the time, it'd be easy, it's just Clojure but with different functions to call, different conventions, but mostly the same. So the next language I'm targeting is Rust. A C-like language, but new concepts around the borrow-checker and lifetimes, something I've been exposed to before, but not this literally.

[+] superdisk|4 years ago|reply
I prescribe Prolog, Lisp, Forth, x86 assembly, and Smalltalk. Each one is remarkably different and not just a coat of paint on top of C (which is 80% of popular languages). You'll have so much fun.
[+] conrs|4 years ago|reply
Work on solving problems using declarative languages and functional languages.

So, either Prolog, or Lisp.

Structure and Interpretation of Computer Programs is a good jumping off point for Lisp while also providing fundamental insights.

Prolog.. honestly, just google "basic" array problems and try and do them in prolog. It takes a mental shift, which is hard at first, and you feel silly not being able to count to 10. But learning how to think in this paradigm is useful - e.g. SQL is declarative.

[+] perth|4 years ago|reply
When you master the hammer you can use it to build stuff, it’s not a competition to know every toy language in existence.
[+] skulk|4 years ago|reply
This project is a curiosity. It may help someone trying to build a quick one-off script (though, then I have the question of why curl wasn't good enough). In any real-world use-case, you can't just plop in some isolated code to make a HTTP request. I haven't looked yet, but I assume this website generates code that blocks the current thread or it spins up its own threadpool; neither of these things are acceptable for any production application.

In my experience, the main concepts behind application architecture remains the same regardless of what language you're using. Concepts like asynchronous execution and how it affects composability and resource management transcend syntax.

[+] brimble|4 years ago|reply
I've found programming language differences less and less interesting the longer I do this. They all do the same thing. Everything's pointers and stacks, usually not that far beneath the surface. Some are more or less ergonomic, but nothing (that sees any widespread use, anyway) is really very different. Just another set of quirks and (probably bad) tools to learn. Library availability & quality matters more to me than what the language looks like.
[+] revicon|4 years ago|reply
Why is language indifference bad? You're correct, most language differences really come down to one language using a colon, the other a semicolon, etc.

Find a problem you feel passionate about solving and then use your skills to go solve it for mankind. You've learned how to be a mason, now go build something beautiful.

[+] SnooSux|4 years ago|reply
Instead of exploring different languages learn about different fields and domains. The language may be basically the same (outside of some more esoteric ones), but Data Science is a very different experience from Web Dev which is different from Embedded Systems. Get out of your comfort zone a little.
[+] yen223|4 years ago|reply
Seek excitement not from the language, but from what you can build with the language
[+] dls2016|4 years ago|reply
chop wood, carry water... or maybe the Turing version: read a symbol, move the head
[+] jraph|4 years ago|reply
With the feature of browsers to copy requests as Curl and now this, it's funny to see curl commands be(com)ing some kind of lingua franca for HTTP request representation!

Now you can play a request and boom, you have your code to make this request in your favorite programming language.

[+] codazoda|4 years ago|reply
It's interesting and I like it. It does seem to be rather opinionated without saying so. The PHP example requires an http library I hadn't heard of (rmccue/requests). Copy/paste of that code doesn't work unless you know how to find and install that library. The related tool they reference, however, does work using the PHP curl library.

I presume they were trying to keep the code to 4 lines or so. The curl library in PHP is rather verbose and requires 5 to 8 lines (depending on if you try to catch the error).

https://www.php.net/manual/en/book.curl.php

[+] NickC_dev|4 years ago|reply
At the time we were using that particular library heavily at WizeHire. I had built the tool for personal convenience. rmccue/requests was likely one of the first "generators".

I expect that we'd accept a PR for PHP curl library implementation. Having a non-third-party option would be valuable.

[+] rgovostes|4 years ago|reply
I wrote http-translator which is one of the linked related projects. It has a smaller feature set, but is just a few hundred lines and can be easily extended to support new frontends (input formats other than curl) and backends (i.e., output languages).

https://ryan.govost.es/http-translator/ | https://github.com/rgov/http-translator

Mine was a project for learning modern-ish JavaScript, and I found that packages for parsing command line arguments in JS were generally poor. I wrote my own shell lexer that is simpler than Eric S. Raymond's in the Python standard library while passing the test suite, and since Burp Suite generates curl commands that use Bash ANSI C strings, I support those too. https://www.npmjs.com/package/shlex

If I were to do further development, I would ditch my curl command line parsing in favor of building curl with Emscripten and having it generate the full HTTP request, then leverage the existing request parser.

curl already supports a —-libcurl flag that generates equivalent C code, and it might be easy to extract the curl_easy_setopt() calls from it.

Both our projects should probably support the built-in urllib.request for Python; Requests isn’t always necessary anymore.

[+] johnx123-up|4 years ago|reply
[+] skywal_l|4 years ago|reply
And if you look to their page you will find an even more complete list:

    curl-to-Go
    curl-to-PHP
    HAR-to-curl
    http-translator (Python, JS and JSON)
    uncurl (to Python)
    hrbrmstr/curlconverter (to R)
    curl-to-postman
    Insomnia
    Paw-cURLImporter
[+] mholt|4 years ago|reply
Curl-to-Go also understands JSON payloads and will even generate the struct definition for you to fill in, so you aren't working with opaque JSON strings.
[+] oefrha|4 years ago|reply
I use Paw[1] for this, which is obviously a lot more powerful by allowing me to easily toggle and/or modify query params, headers, edit body as structured JSON / XML / application/x-www-form-urlencoded / multipart/form-data, etc. Probably doable in other API clients like Insomnia[2], too.

[1] https://paw.cloud/

[2] https://insomnia.rest/

[+] reese_john|4 years ago|reply
Postman enables you to convert your HTTP requests to several languages as well.
[+] folli|4 years ago|reply
As a Java hobbyist, I'm dying seeing the difference between the Java boilerplate and the Python two-liner
[+] pcdavid|4 years ago|reply
This is not representattive of modern Java, which has much better APIs. Since Java 11:

  public HttpResponse<String> fetch(String url) {
    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
    return client.send(request, BodyHandlers.ofString());
  }
[+] lambic|4 years ago|reply
I feel like more granular options would be useful here like "Python with beautifulsoup" or "PHP with Guzzle".
[+] blueflow|4 years ago|reply
Why use Guzzle at all when you can use the PHP libcurl bindings directly? Guzzle is translating to libcurl either way...
[+] steerablesafe|4 years ago|reply
I expected C with libcurl on the list, but in a sense that might be the toughest to implement.
[+] jandrese|4 years ago|reply
Seems like it should be pretty straightforward. curl_easy_init(), ..._setopt(), ..._perform(), ..._cleanup() covers the use cases I think.

libcurl only gets weird when you're threading multiple fetches or integrating with a GUI toolkit or something. Even then it's not hard at all to integrate with GTK unless you need absolute peak performance or are handling tens of thousands of simultaneous connections.

[+] tyree731|4 years ago|reply
Maybe it’s hard to implement, but it’s implementation is a standard part of the curl tool:

$ curl —-libcurl curl.c ...

[+] sethammons|4 years ago|reply
I haven't touched it in years, but I made a utility to do the opposite in Go. Start with Go's request and transform it into a curl call. It was helpful for testing at the time. Looking at it, wow, that was 7 years ago!

https://github.com/sethgrid/gencurl

[+] benbristow|4 years ago|reply
Something a bit different but I'm a huge fan of NSwag recently.

https://github.com/RicoSuter/NSwag

We use it on a project at work in C# and I've also been using it personally on pet projects with TypeScript.

Allows you to automatically generate REST API clients based on Swagger/OpenAPI specs and is very customizable.

Really nice if building out microservices and you're wanting to communicate between each other or building web apps and want to communicate with APIs. Saves a lot of time writing code to make requests and handle different status codes etc. Automatically generates all the classes/interfaces with sane naming schemes etc.

Even has a GUI application to generate the configs also if you wish.

[+] capableweb|4 years ago|reply
I agree with the comments saying it should say "Language + Client library" instead of just language, as there are many ways of doing requests with different libraries.

Also, funny enough, the first command I tried didn't work as I expected, but thinking about it, it also seems hard to implement correctly. The cURL command I tried was "curl -v https://google.com/" but none of the implementations include verbose logging.

Weirdly, JSON is listed there, and it is absolutely not clear how JSON can be considered a language that can make HTTP requests. I wonder what client library that is?

[+] verhovsky|4 years ago|reply
The JSON output just dumps curlconverter's internal representation of a parsed curl command. You could use it if you want to use curlconverter to parse curl commands and then do stuff with that parsing yourself in a language other than JavaScript. You would install the command line tool then use your language's command-running library. For example, from Python:

    import subprocess
    import json

    result = subprocess.run(['curlconverter', '--language', 'json', 'example.com'], capture_output=True, text=True)
    command = json.loads(result.stdout)
    print(command)
[+] kevinsundar|4 years ago|reply
I always thought this would be a cool feature to have in a http library. The library knows the best way to convert a curl command to its own internal representation. Something like requests.from_curl()
[+] beranabus|4 years ago|reply
The go version seems to be missing timeouts. There is none by default and the --connect-timeout flag is not setting one either.
[+] chockchocschoir|4 years ago|reply
Rightly so. Unless you specify `--max-time` in your cURL invocation, all the examples should be missing timeouts, as cURL by default doesn't have any timeouts, only if you specify `--max-time N`.
[+] verhovsky|4 years ago|reply
I work on this tool (but I'm not the original author), feel free to ask me questions.